There are 5 original values in js, 6 types of judging, and 9 native built-in constructors.
This 569 forms the basis of the js language.
The 5 original values are: numbers, characters, booleans, null, undefined
typeof can judge: numbers, characters, booleans, object, function, undefined. Note that null and arrays, typeof both output object.
typeof cannot distinguish arrays and objects. How to judge types? Use Object.prototype.toString.apply().
if(value&&typeof value ==='object'&&value.constructor === Array)
If the above detection is created in different frames and windows, false will be given, and the window object will be different.
A reliable method is if(Object.prototype.toString.apply(value)==="[object Array]")
arguments array is not an array, it is just an object with a length member attribute.
As shown in the following example, arguments are not ordinary arrays
The code copy is as follows:
var a = function (){
var b = Object.prototype.toString.apply(arguments);
console.log(b);
}
a();//Output [object Arguments]
The code copy is as follows:
var a = function (){
var c = [];
var b = Object.prototype.toString.apply(c);
console.log(b);
}
a();//Output [object Array]
How does instanceof determine whether an instance is an instance
The properties in prototype have constructor.
The default prototype property is an object object that can be set to any complex value, ignore setting to the original value.
Although he is all an object, he is special, and the circular chain links each instance to the prototype property of its constructor. There is a hidden link between the instance and the prototype attribute of the constructor, which is the instance's __proto__. At the same time, the constructor attribute of the instance is obtained through the constructor of the constructor of the constructor function prototype.
However, we must retain the constructor, so that the new instance can have the constructor attribute, and we can also use instanceof to judge.
The code copy is as follows:
var Foo = function(){}
Foo.prototype={constructor:Foo}
var FooInstance = new Foo;
FooInstance.__proto__=== Foo.prototype;//true
FooInstance.constructor === Foo; //true
In fact, the instanceof judgment is not based on the constructor, but on the prototype chain, as shown in the following example
The code copy is as follows:
var Foo = function(){};
Foo.prototype={};
var FooInstance = {};
FooInstance.__proto__=Foo.prototype;
console.log(FooInstance instanceof Foo);//true
Use original values, no constructors
Which values are false: false,"",null,0,-0,NaN,undefined, these are false, others are true
But please pay attention to the following example
The code copy is as follows:
var a = Boolean(false);
var b = new Boolean("");
if (a ){console.log(a);}//Cannot output
if (b ){console.log(b);}//Boolean {[[PrimitiveValue]]: false} new One is equivalent to an object, it is not false
The above article is a little more theoretical, but these are the basis for the JavaScript language, and you must understand it clearly.