This article mainly introduces the relevant information ofJavaScriptstandard objects in detail, which has certain reference value. Interested friends can refer to
In the world of JavaScript Here, everything is an object.
But some objects are still different from other objects. In order to distinguish the type of object, we use typeofoperatorto get the type of object, which always returns astring:
typeof 123; // 'number' typeof NaN; // 'number' typeof 'str'; // 'string' typeof true; // 'boolean' typeof undefined; // 'undefined' typeof Math.abs; // 'function' typeof null; // 'object' typeof []; // 'object' typeof {}; // 'object'
It can be seen thatnumber, string, boolean, functionandundefinedare different from other types. Pay special attention to the fact that the type of null is object, and the type of Array is also object. If we use typeof, we will not be able to distinguish between null, Array and object in the usual sense - {}.
Packaging objects
In addition to these types, JavaScript also provides packaging objects. Friends who are familiar with Java must be very aware of int andIntegerThis ambiguous relationship.
Number, boolean and string all have packaging objects. Yes, in JavaScript, strings also distinguish between the string type and its wrapper type. The packaging object is created with new:
var n = new Number(123); // 123,生成了新的包装类型 var b = new Boolean(true); // true,生成了新的包装类型 var s = new String('str'); // 'str',生成了新的包装类型
Although the packaging object looks exactly the same as the original value and is displayed exactly the same, their type has changed to object ! Therefore, comparing the wrapped object with the original value using === will return false:
typeof new Number(123); // 'object' new Number(123) === 123; // false typeof new Boolean(true); // 'object' new Boolean(true) === true; // false typeof new String('str'); // 'object' new String('str') === 'str'; // false
So don’t use wrapped objects if you are idle! Especially for thestringtype! ! !
If we do not writenewwhen usingNumber, BooleanandStringWhat happens?
At this time,Number(), BooleanandString()are treated as ordinary functions to convert any type of data Fornumber, booleanandstringtypes (note not their packaging type):
var n = Number('123'); // 123,相当于parseInt()或parseFloat() typeof n; // 'number' var b = Boolean('true'); // true typeof b; // 'boolean' var b2 = Boolean('false'); // true! 'false'字符串转换结果为true!因为它是非空字符串! var b3 = Boolean(''); // false var s = String(123.45); // '123.45' typeof s; // 'string'
Do you feel like your head is getting big? This is the unique hypnotic charm of JavaScript!
To summarize, there are several rules that need to be followed:
Do not usenew Number(), new Boolean(), new String()Create a packaging object;
UseparseInt()orparseFloat()to convert any type tonumber;
UseString()to convert any type tostring, or directly calltoString()# of an object ##Method;
typetobooleanbefore judging, because you can # directly##Write if (myVar) {...};
typeofoperator Can judgenumber, boolean, string, function and undefined;
ArrayTo useArray.isArray(arr);
##JudgePlease usemyVar === null;
To determine whether a global variable exists, useThe internal function uses
method?nullandundefinedare not available! Indeed, these two special values must be excluded, although null is also disguised as theobject classtype.
更细心的同学指出,number对象调用toString()报SyntaxError: