Home >Web Front-end >JS Tutorial >Detailed explanation of javascript basic types_basic knowledge
There are a total of 5 primitive values in js, 6 that can be judged by typeof, and 9 native built-in constructors.
These 569 form the basis of js language.
The 5 primitive values are: numbers, characters, Boolean, null, undefined
Typeof can judge: numbers, characters, Boolean, object, function, undefined. Note that null and array, typeeof both output object.
typeof cannot distinguish between arrays and objects. How to determine the type? Use Object.prototype.toString.apply().
if(value&&typeof value ==='object'&&value.constructor === Array)
The above detection will give false if the array is created in different frames and windows, and the window object is different.
The reliable method is if(Object.prototype.toString.apply(value)==="[object Array]")
The arguments array is not an array, it is just an object with a length member property.
As shown in the following example, arguments are not ordinary array
}
a();//Output [object Arguments]
}
a();//Output [object Array]
How does instanceof determine whether it is an instance?
The attributes in prototype include constructor.
The default prototype attribute is an object object and can be set to any complex value, ignoring the original value.
Although it is an object, it is special, and the circular chain links each instance to the prototype attribute of its constructor. There is a hidden link between the instance and the constructor's prototype property, which is the instance's __proto__. At the same time, the constructor attribute of the instance is obtained through the constructor of the constructor prototype.
But keep the constructor, so that the new instance has the attributes of the constructor, and you can also use instanceof to judge.
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
Use primitive values, no constructors
Which values are considered false: false, "", null, 0, -0, NaN, undefined, these are considered false, the others are true
But please pay attention to the following example
The above article is a little more theoretical, but these are the basis of the JavaScript language and must be understood clearly.