Detailed introduction to JavaScript standard objects

零下一度
Release: 2017-06-28 13:34:19
Original
1224 people have browsed it

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'
Copy after login

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',生成了新的包装类型
Copy after login

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
Copy after login

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'
Copy after login

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;

  • Usually there is no need to convert any

    typetobooleanbefore judging, because you can # directly##Write if (myVar) {...};

  • typeofoperator Can judgenumber, boolean, string, function and undefined;

  • Judge
  • ArrayTo useArray.isArray(arr);

    ##Judge
  • null

    Please usemyVar === null;

    To determine whether a global variable exists, use
  • typeof window. myVar === 'undefined';

    The internal function uses
  • typeof myVar === 'undefined' to determine whether a variable exists .


    Finally, an attentive student pointed out that does any object have a
toString()

method?nullandundefinedare not available! Indeed, these two special values must be excluded, although null is also disguised as theobject classtype.

更细心的同学指出,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!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!