JavaScript Wrapper object Instance Analysis _javascript skill

Source: Internet
Author: User
Tags wrapper

The examples in this article describe the use of JavaScript wrapper objects. Share to everyone for your reference. The specific analysis is as follows:

A JS object is a compound value: it is a property or a named Worthy collection.

Refer to the following code:

var s = "Hello World";
var len = s.length;

In this example, S is a string, and the string is not an object, but why is there a property? In fact, as long as the attribute of the string s is referenced, JS converts the string to an object by calling the new string (s), which inherits the method of the string and is used to handle the property's reference, and once the property reference is finished, The newly created object is destroyed (the temporary object is not necessarily created or destroyed on implementation, but the whole process appears to be the case).

As with strings, numbers and Boolean values also have their own methods of creating temporary objects through the number () single-Core Boolean () constructor, which are invoked from this temporary object, whereas null and undefined do not wrap objects: Accessing their properties can result in a type error.
For example, the following code:

var s0 = "Hello World";
S0.len = m;
var t = S.len; The value of T will be undefined

Because line 2nd creates a temporary object, it is destroyed immediately, and the third line creates a new string object from the original string value, trying to read its Len property, which does not exist. This code shows that when reading the property values or methods of strings, numbers, and Boolean values, it behaves like an object. But if you try to assign a value to its properties, the action is ignored: The modification only occurs on the temporary object, and the temporary object is not persisted.
Temporary objects that are temporarily created when accessing properties of strings, numbers, or Boolean values are called wrapper objects.
We can show that we create a string object and then add its properties, and naturally this property is preserved all the time:

var str = "Hello world";
var objs = new String (str);
Objs.len = m;
var t = Objs.len; T will be assigned a value of 100

JS converts the wrapper object to its original value when necessary, so it shows that the object created and its corresponding original value often but not always behaves the same. the = = Operator treats the original value as equal to its wrapper object, but = = = The equality operator treats them as unequal, and the TypeOf operator lets you see the difference between the original value and the wrapper object.

I hope this article will help you with your JavaScript programming.

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.