Introduction to packaging objects in JavaScript, and javascript packaging objects

Source: Internet
Author: User

Introduction to packaging objects in JavaScript, and javascript packaging objects

A javascript Object is a composite value. It is a set of attributes or named values and uses the symbol "." To reference attribute values. When the attribute value is a function, it is called a method. We can see that the string also has attributes and methods:
Copy codeThe Code is as follows:
Var s = "hello, world! ";
Var word = s. substring (s. indexof ("") + 1, s. length );

Since a String is not an object, why does it have attributes? If the attribute of String s is referenced, javascript will convert the String value to an object by calling the new String (s) constructor, this object inherits the string method and is used to process the attribute reference. Once the attribute reference ends, this newly created object will be destroyed (in fact, this object may not be created in implementation, but the whole process looks like this ).

Like a string, numbers and Boolean values also have their own methods: create a temporary object through the Number () and Boolean () constructor, and these methods are called from this temporary object. This temporary object is called a packaging object.

Note:
Copy codeThe Code is as follows:
Var s = "test"; // declare a string
S. len = 4; // set a len attribute for it
Var t = s. len; // query this attribute

At this time, the output t should be undefined. The second line of code creates a temporary String object and assigns the len attribute to 4. Then, the object is destroyed, the third line of code sets a new attribute through the original string value s and tries to read its len attribute. This attribute does not exist, so the t output value is undefined.

This Code indicates that when reading numbers, strings, and Boolean value attribute values (or methods), it looks like an object, but when trying to assign values to its properties, this operation will be ignored: the modification only happens to the temporary object, which is not retained.

The temporary object created when you access the properties of a string, number, or Boolean value is called a packaging object. It is only occasionally used to distinguish between string value and string object, number and value object, Boolean value and Boolean object.

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.