Home > Web Front-end > JS Tutorial > body text

A brief discussion on the difference between js object properties through dots (.) and square brackets ([])

高洛峰
Release: 2017-02-08 17:40:44
Original
985 people have browsed it

[JS object attribute query and setting]

You can get the value of the attribute through the dot (.) or square bracket ([]) operator. The left side of the operator should be an expression that returns an object. For dots (.), the right-hand 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. This string is the name of the attribute:

<script type="text/javascript">
 var author = book.author; //得到book的"author"属性
 var name = author.subname; //得到author的"surname"属性
 var title = book["main title"]; //得到book的"main title"属性
</script>
Copy after login

When through dot operations (.) 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.

Conversely, when using [] to specify the properties of an object, the property name is represented by a string. Strings are the data type of js, and they can be modified and created while the program is running.

<script type="text/javascript">
 var addr = "";
 for(i=0;i<4;i++){
  addr += cutomer["address" + i] + "\n";
 }
</script>
Copy after login

This code reads the address0, adddress1, address2, adddress3 properties of the customer object and connects them.

The above is the editor’s brief discussion on the differences between js object attributes through dots (.) and square brackets ([]). I hope you will support the PHP Chinese website~

For more on the differences between js object attributes through dots (.) and square brackets ([]), please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!