There are five simple data types (undefined, null, boolean, number, string) and one complex type (Object) in ECMAScript
So my question is:
var bol = true; var num = 1; var str = 'abc'Of which
bol,num,strIs it an object or a basic type?
The following is the test I did. Great God can ignore it:
var num = 1; var numObj = new Number(1); console.log(num instanceof Number); // false console.log(num.constructor); // Number console.log(numObj instanceof Number); // true console.log(numObj.constructor); // Number var bol = true; var bolObj = new Boolean(true); console.log(bol instanceof Boolean); // false console.log(bol.constructor); // Boolean console.log(bolObj instanceof Boolean); // true console.log(bolObj.constructor); // Boolean var str = "abc"; var strObj = new String("abc"); console.log(str instanceof String); // false console.log(str.constructor); // String console.log(strObj instanceof String); // true console.log(strObj.constructor); // String
There is a lot of doubt in the test: the values of the basic types are not examples of the corresponding packaging types, but
constructorHowever, it points to the corresponding packaging type. .
“Secret” inside’s statement that all variables are objects is questionable.
Simple primitive types (string, boolean, number, null, undefined) are not objects themselves
var num = 1; typeof a // "number"
Num is the base type number, not object.
As can be seen from your code below
console.log(num instanceof Number); // false
So the first question is
console.log(num.constructor); // Number
In fact, the Number constructor is called by default behind num to construct a Number wrapper type (this wrapper type is actually very similar to that of Java, except that JS wrapper type is completed by calling the constructor Number), thus making num an object, which is the case above
No, but the constructor is called only when it is used as an object (calling properties, methods). your code num.constructor returns Number, which clearly illustrates this problem.