Liaoche JS Tutorial Note 7 Basic types and package types

Source: Internet
Author: User

In the JavaScript world, everything is an object.

Some objects, however, are not the same as other objects. To distinguish the type of an object, we use the typeof operator to get the type of the 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 '             

Visible,,,, number string boolean function and undefined distinct from other types. The type null of special attention object is Array Also, object if we use the typeof object--that will be indistinguishable null , Array and in general sense {} .

Wrapping Object

In addition to these types, JavaScript also provides packaging objects, and the familiarity of Java's small partners must be clear int and Integer this ambiguous relationship.

number, boolean and string both have packaging objects. Yes, in JavaScript, a string also distinguishes between string a type and its wrapper type. The wrapper object is new created with:

var n = new Number(123); // 123,生成了新的包装类型var b = new Boolean(true); // true,生成了新的包装类型var s = new String(‘str‘); // ‘str‘,生成了新的包装类型

Although the wrapper object looks exactly the same as the original value, the display is identical, but their type has changed object ! Therefore, the wrapper object and the original value === are returned with a comparison false :

 typeof new number ( 123); //' object ' new number (123) = = = 123; //falsetypeof new Boolean ( true); //' object ' new Boolean (true) = = = true; //falsetypeof new String (//' object ' new String ( ' str ') = = =  ' str '; //false              

So do not use the packaging object of the pain of idle eggs ! Especially for string type!!!

What happens if we Number Boolean String do not write when we use, and when new ?

At this point,, Number() Boolean and as a String() normal function, convert any type of data to number , boolean and string type (note not its wrapper type):

var n = Number(‘123‘); // 123,相当于parseInt()或parseFloat()typeof n; // ‘number‘var b = Boolean(‘true‘); // truetypeof b; // ‘boolean‘var b2 = Boolean(‘false‘); // true! ‘false‘字符串转换结果为true!因为它是非空字符串!var b3 = Boolean(‘‘); // falsevar s = String(123.45); // ‘123.45‘typeof s; // ‘string‘

Did you feel a big head? This is the hypnotic charm peculiar to JavaScript!

To summarize, there are a few rules to follow:

    • Do not new Number() use new Boolean() , new String() create packaging objects;

    • Use parseInt() or parseFloat() to convert any type to number ;

    • The String() method used to convert any type to string , or directly invoke, an object toString() ;

    • It is not usually necessary to convert any type to boolean re-judgment, because it can be written directly if (myVar) {...} ;

    • typeofOperators can determine the,,, number boolean string function and undefined ;

    • Judge Array to use Array.isArray(arr) ;

    • Judging null please use myVar === null ;

    • Determine if a global variable exists typeof window.myVar === ‘undefined‘ ;

    • The function internally determines whether a variable exists typeof myVar === ‘undefined‘ .

Finally have the careful classmate pointed out, any object has the toString() method? nulland undefined there is no! This is true, except for these two special values, although null they are also disguised as object types.

The more careful classmate points out that the number object calls the toString() SyntaxError:

123.toString(); // SyntaxError

In this case, special treatment is needed:

123..toString(); // ‘123‘, 注意是两个点!(123).toString(); // ‘123‘

Liaoche JS Tutorial Note 7 Basic types and package types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.