Introduction to Primitive object encapsulation in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the Primitive object encapsulation in JavaScript. This article focuses on the encapsulation process and provides sample code. If you need it, refer to the JavaScript section, string, number, and boolean are basic primitive types, that is, strings, values, and boolean values do not exist in the form of objects. However, JavaScript will automatically encapsulate these three types of values for attributes and methods as objects because these three primitive type values need to be operated. Taking string as an example, the encapsulation process is as follows:

1. When JavaScript accesses the string value attributes or calls a method, it will call new String (string value) to automatically encapsulate the String into a string object.
2. JavaScript will access the attributes or methods of the newly created object and return corresponding results.
3. After the property access or method call is completed, JavaScript will immediately destroy the newly created object.

The following code is used as an example. It does not make sense to write properties to the String object automatically created by JavaScript, because the created object no longer exists after the Write statement is completed:


The Code is as follows:


Var s = "test ";
S. length = 9;
Console. log (s. length); // still 4
S. newVariable = 9;
Console. log (s. newVariable); // undefined
Console. log (s = "test"); // true

It is worth noting that the s variable in the above Code always represents the primitive string, and the string object automatically created by JavaScript exists in the execution of s. length or s. during the newVariable operation. This can be verified from the last line of code in the above experiment.

In addition to automatic encapsulation of Primitive values, developers can also choose to manually perform the corresponding process. Unlike automatic encapsulation, manually encapsulated objects are not destroyed immediately. Therefore, attribute write operations for manually encapsulated objects are meaningful:


The Code is as follows:


Var t = new String ("test ");
T. length = 9;
Console. log (t. length); // still 4, as length attribute is read only
T. newVariable = 9;
Console. log (t. newVariable); // 9

Console. log (t = "test"); // true
Console. log (t = "test"); // false

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.