Some of the phenomena I don't know about JavaScript Object

Source: Internet
Author: User
Tags access properties object object

1, we all know that there are two methods of object access properties, one is to use. operator, the name of the property to be added using it must satisfy the naming specification of the identifier, and the method accessed using the [] key can accept any Utf-8/unicode string as the property name, and in ES6 It can also add a computable property name.

objects use the [] key to access a method different from the array subscript [] access, the object uses the [] key to access, the property name is converted to a string form, and then to find access, even if the property name is a number or a number is no exception.

The following examples can be observed:

var obj = {a:1,b:2,c:3= ' this is obj ';

Output obj:

var obj1 = {d:4,e:5,f:6= ' this is obj1 ';

Output obj:

From the above code we can see that the object is accessed using the [] key. The property name is converted to the form of a string, that is, a obj.tostring () operation

So the property we add to the object is not obj but ' [Object Object] '. Therefore, when the second assignment is obj[obj1], only the ' [Object Object] ' property is modified.

2. How to use the computed attribute name in ES6:

In the normal object literal, the attribute name, whether or not added quotation marks, is treated by default as a string, so the variable cannot be used in the object's literal, which resolves the problem in ES6.

Normal object literal defines the object:

var myObj = {     1,     2,     3}

Use the form of a computable property name:

var str = ' str '; var obj = {    [str + ' 1 ']:1,    [str + ' 2 ']:2,    [str+ ' 3 ']:3};

Print obj:

Some of the phenomena I don't know about JavaScript Object

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.