JavaScript standard objects and wrapper objects

Source: Internet
Author: User
Tags wrapper

Standard Object

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 //' function ' typeof NULL //' object ' typeof //' object ' typeof //' object '

As can be seen, number, string, Boolean, function, and undefined are different from other types. It is particularly important to note that the type of NULL is Object,array and object, and if we use typeof, we will not be able to distinguish between null, array, and object--{} in the usual sense.

Wrapping Object

In addition to these types, JavaScript also provides a wrapper object, the familiar Java of the small partners must be very clear int and integer this ambiguous relationship.

Number, Boolean, and string all have wrapper objects. Yes, in JavaScript, strings are also distinguished by the string type and its wrapper type. The wrapper object is created with new:

var New Number (123//123, generates a new wrapper type var New Boolean (true//True, generates a new wrapper type  var New String (' str'//' str ', resulting in a newer wrapper type

Although the wrapper object looks exactly the same as the original value, the display is identical, but their type has become Object! Therefore, the wrapper object and the original value are compared with = = = to return false:

typeof New Number (123//' object 'new number (123123//Falsetypeof  New Boolean (true//' object 'new Boolean (truetrue)  //Falsetypeofnew String (' str '//' object 'new String (' str'str'//False

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

What happens if we do not write new when we use number, Boolean, and string?

At this point, Number (), Boolean, and string () are treated as normal functions, converting any type of data to number, Boolean, and string type (note that it is not a wrapper type):

var n = number (' 123 '//123, equivalent to parseint () or parsefloat ()typeof//' number ' var b = Boolean (' true '//Truetypeof//' Boolean 'var b2 = Boolean (' false '//true! ' False ' string conversion result to true! Because it is a non-empty string!  var b3 = Boolean ("///Falsevar s = String (123.45//') 123.45 'typeof//' 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 use new number (), New Boolean (), New String () to create the wrapper object;

    • Use parseint () or parsefloat () to convert any type to number;

    • Use String () to convert any type to string, or call the ToString () method of an object directly;

    • It is not usually necessary to convert any type to Boolean again, because you can write directly if (MyVar) {...} ;

    • The typeof operator can determine number, Boolean, String, function, and undefined;

    • Determine the array to use Array.isarray (arr);

    • To determine null, use MyVar = = = NULL;

    • Determine if a global variable exists with typeof Window.myvar = = = ' undefined ';

    • The function internally determines whether a variable exists with typeof MyVar = = = ' undefined '.

Finally, a careful classmate pointed out that any object has the ToString () method? Null and undefined there is no! This is true, except for these two special values, although NULL is also disguised as the object type.

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

123 // SyntaxError

In this case, special treatment is needed:

123 // ' 123 ', attention is two points! (123//' 123 '

Don't ask why, this is the fun of JavaScript code!

JavaScript standard objects and wrapper objects

Related Article

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.