An issue in which attribute names are quoted and unquoted when the object is declared by JS

Source: Internet
Author: User

In the case of a property name with quotation marks and no quotation marks are all possible, the effect is the same.

var obj = {      name    ' Hello ',      ' age '   : 1,  };     document.write (obj[' name '] + ' <br/> ' );  document.write (obj.age);  

The above two lines of code can be executed correctly.

When and only if your property name is an illegal and weird name, you will get an error.

var obj = {      333: ' This will be an    error '
document.write (obj.333);  

Error at this time.

var obj = {      "333": ' This will also be an error '  }; document.write (obj. 333);  

If the property name is a number, you must have double quotation marks and access with [] square brackets.

var obj = {    "333": ' This is correct '};console.log (obj["333"]);

Conclusion: Using the legal attribute name, using . and [] access are OK;

If the property name is a number, it must be surrounded by "" and accessed with [] square brackets.

An issue in which attribute names are quoted and unquoted when the object is declared by JS

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.