A brief discussion on the differences of JS object properties by point (.) and square brackets ([]) _javascript Skills

Source: Internet
Author: User

"Query and Settings for JS object Properties"

You can get the value of a property by using the dot (.) or square brackets ([]) operator. The left side of the operator should be an expression that returns an object. For dots (.) , the right side must be a simple identifier named after the property name. for square brackets ([]), the square brackets must be an expression that evaluates to a string, which is the name of the attribute:

<script type= "Text/javascript" >
 var author = book.author;//Get book's "author" attribute
 var name = Author.subname; Get author's "surname" attribute
 var title = book["Main title"]//Get book's "Main title" Property
</script>

When passing through the dot operator (.) Method object property, the property name is represented by an identifier. Identifiers must appear directly in the JS program, they are not data types, so the program cannot modify them.

In turn, property names are represented by strings when the properties of an object are passed through []. Strings are data types of JS, which can be modified and created when the program runs.

<script type= "Text/javascript" >
 var addr = "";
 for (i=0;i<4;i++) {
  addr + = cutomer["Address" + i] + "\ n";
 }
</script>

This code reads the ADDRESS0,ADDDRESS1,ADDRESS2,ADDDRESS3 properties of the customer object and joins them.

The above is a small series for everyone to talk about the JS object properties through the point (.) and square brackets ([]) of the different parts of the whole, I hope that we support the cloud-Habitat Community ~

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.