Javascript packaging object Example Analysis _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces javascript packaging objects. Examples of this article analyze the javascript Object operation skills and have some reference value. If you need it, you can refer to the example below to describe how to use javascript packaging objects. Share it with you for your reference. The specific analysis is as follows:

A js object is a composite value: it is a set of attributes or named values.

Refer to the following code:

var s = "hello world";var len = s.length;

In this example, s is a string, but the string is not an object. Why is there an attribute? In fact, as long as the attribute of String s is referenced, js will convert the String to an object by calling new String (s). This object inherits the String method, and is used to process the attribute reference. Once the attribute reference ends, the newly created object will be destroyed (this temporary object is not necessarily created or destroyed in implementation, but the whole process seems like this ).

Like a string, numbers and Boolean values also have their own Methods: Using Number () Single-core Boolean () constructor to create a temporary object, these methods are called from this temporary object; however, null and undefined do not wrap objects: access to their properties may cause type errors.
For example, the following code:

Var s0 = "hello world"; s0.len = 100; var t = s. len; // t value will be undefined

This is because the row 3 creates a temporary object and destroys it immediately. The Row 3 creates a new String object using the original string value and tries to read its len attribute, which naturally does not exist. This Code shows that when reading the attribute values or methods of strings, numbers, and boolean values, they behave like objects. However, if you try to assign a value to its attribute, this operation will be ignored: the modification only happens to the temporary object, and the temporary object is not retained.
A temporary object is created temporarily when you access the properties of a string, number, or Boolean value.
We can create a String object and add its attributes. Naturally, this attribute will be retained all the time:

Var str = "hello world"; var objs = new String (str); objs. len = 100; var t = objs. len; // t will be assigned 100

Js will convert the wrapped object to the original value when necessary, so it shows that the created object and its corresponding original value are often but not always the same. The = Operator treats the original value and its packaged object as equal, but the = equal operator treats them as unequal. In addition, the typeof operator can see the difference between the original value and the packaged object.

I hope this article will help you design javascript programs.

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.