Objects in JavaScript (1)

Source: Internet
Author: User

Recently, I talked to my friends about objects in JavaScript. My friends write JavaScript For a living and have a good life. However, I found that he does not really understand some of the core features of the language. If you are confused, I will try to answer the question below.

A friend quoted an explanation in a book as follows:
"What's interesting is that the original ECMAScript values (such as boolean, number, and string) are pseudo objects, that is, they have attributes and Methods ."
Sorry, he does not.
Let's first look at an example.
Var a = 5;
A. t = 3;
Alert (a. t); in this example, alert "undefined" is used ". Why? If a is a pseudo object, why is the property not saved? In fact, a is not an object, or even a pseudo object. It is the original number. It has no attributes. Many others JavaScript automatically convert variables from one type to another. See the following code snippet:
Var B = "w" + a + [1, 2, 3]; number a and array [1, 2, 3] are automatically converted to string. The same thing occurs before the dot (.) operator. JavaScript simply converts the operand on the left to the object. Therefore, the second line in the example creates a Number object, whose value is equal to a (5 in this example), and then assigns 3 values to the t attribute of the newly created Number object. However, the new Number object will not replace variable a (it will not be written back to variable a) and will only be reclaimed by the garbage collector. The third row creates a new object again, and then tries to read the t attribute of the new object. The t attribute is "undefined ".
The original types (boolean, number, and string) are not objects. They may be converted like objects. What is the conversion rule? JavaScript has six built-in types: null, undefined, number, string, boolean, and object. The conversion rules are as follows:
1. If it is an object, it remains unchanged.
2. If it is null or undefined, an exception is thrown.
3. Otherwise, create (new Number (input), new String (input), or new Boolean (input )).
I hope this article will help you understand JavaScript objects. The next article will write the prototype.

Translator:
Var a = 2;
Var B = new Number (2 );
Alert (a = B); // true
Alert (a = B); // false
Alert (a. valueOf (); // 2
Alert (B. valueOf (); // 2

 

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.