Home >Web Front-end >JS Tutorial >Detailed explanation of javascript basic types_basic knowledge

Detailed explanation of javascript basic types_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:29:501402browse

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

Copy code The code is as follows:

var a = function (){
var b = Object.prototype.toString.apply(arguments);
console.log(b);

}
a();//Output [object Arguments]

Copy code The code 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 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.

Copy code The code 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

Copy code The code is as follows:

var Foo = function(){};
Foo.prototype={};
var FooInstance = {};
FooInstance.__proto__=Foo.prototype;
console.log(FooInstance instanceof Foo);//true

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

Copy code The code 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 is equivalent to an object, it is not false

The above article is a little more theoretical, but these are the basis of the JavaScript language and must be understood clearly.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn