This article mainly introduces the relevant information of JavaScript standard 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 typeof operator to get the type of object, which always returns a string:
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 that number, string, boolean, function and undefined are 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 and IntegerThis 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 the string type! ! !
If we do not write new when using Number, Boolean and String What happens?
At this time, Number(), Boolean and String() are treated as ordinary functions to convert any type of data For number, boolean and string types (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 use new Number(), new Boolean(), new String()Create a packaging object;
Use parseInt() or parseFloat() to convert any type to number;
Use String() to convert any type to string, or directly call toString()# of an object ##Method;
type to boolean before judging, because you can # directly ##Write if (myVar) {...};
typeofoperator Can judge number, boolean, string, function and undefined;
ArrayTo useArray.isArray(arr);
##JudgePlease use myVar === null;
To determine whether a global variable exists, useThe internal function uses
method? null and undefined are not available! Indeed, these two special values must be excluded, although null is also disguised as the object class type. 更细心的同学指出,number对象调用toString()报SyntaxError: 遇到这种情况,要特殊处理一下: 不要问为什么,这就是JavaScript代码的乐趣! The above is the detailed content of Detailed introduction to JavaScript standard objects. For more information, please follow other related articles on the PHP Chinese website!123.toString(); // SyntaxError
123..toString(); // '123', 注意是两个点!
(123).toString(); // '123'