Some of the most confusing basic data types in JavaScript are those of built-in wrapper objects

Source: Internet
Author: User

The difference between a data type and an object:

The basic data types are not properties and methods, but objects have; just as the basic data types in Java are the same as reference types;

Look at an example:

var s= "xxxx"

var xxxx=s.substring (S.lastindexof ("") +1,s.length);

First you have to understand that in JS the string is the basic type, as the number type. Compared to Java, does its basic type have this method call? Of course not. Java string is an object, so you can tune it. It would be impossible to replace it with Int.

We can use the TypeOf operator to see the data type of the string, we can enter the following code in the browser address bar and return to view the results

Javascript:var s= ""; typeof (S)

The result is of course string.

And the object type is objects. Are you confused? So why is the manipulation of strings using object notation?

In fact, the three key basic types in JS have a corresponding object class. That is, JS also supports number, String, and Boolean classes, which are the wrapper classes for those basic data types, or wrapper. Wrapper is not only the same as the value of the base type, but also encapsulates some of the properties and methods associated with the data.

JS can be very flexible to convert one type to another type, when we use the string in the object environment, JS will create a string wrapper for this value inside the object. The string object replaces the original string. The same is true of the other two types. We use the string in the object environment to be aware that this string object is only instantaneous, it allows us to access the property or method, and then it is useless, so the system will discard it.

Like what:

var len=s.length;

Suppose that s exists and is a string. Then a new string object is created during the execution of the above statement so that the length property can be accessed, which is instantaneous. and S will not have any change. It's like a stunt in a movie. Everyone on earth can understand it.

If you want to use the string object explicitly in JS, you have to use the new operator. Such as:

var s = "Hello World";//string base type primitive

var s= new string ("Hello World");//String Object

The functions and operations of these two s are almost identical. Probably only typeof can recognize the true sun Wukong.

In turn, when the string object s is a + operation with the base type string, the string object is automatically converted to the base string type in an environment where the original string is required.

such as msg= s+ "!"; At this time s secretly by a primitive type string alias to complete the operation process, itself is a string object, but if it is

s = s+ "!"; Then s becomes the basic type string;

Remember that the other two types, number and Boolean, are the same principle as String.

Some of the most confusing basic data types in JavaScript are those of built-in 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.