{}, [] Brackets in js.

Source: Internet
Author: User

1. braces {} indicate defining an object. In most cases, there must be a pair of attributes and values or functions.
For example, var LangShen = {"Name": "Langshen", "AGE": "28 "};
The above declares an object named "LangShen". multiple attributes or functions are separated by commas (,) because they are attributes of objects,
Therefore, you should use. (point) layer-by-layer access: LangShen. name, LangShen. AGE, of course, we can also use Arrays for access, such as LangShen ["Name"] And LangShen ["AGE"]. The results are the same.
This method is often used in JSON data structures. In addition, we often use it when writing function groups, such:
Copy codeThe Code is as follows:
Var LangShen = {
Name = function (){
Return "LangShen ";
},
Age = function (){
Return "28 ";
}
}

The call method is similar. Because it is a function group, add (), for example, alert (LangShen. Name ());
2. [] brackets indicate an array, which can also be understood as an array object.
For example, var LangShen = ["Name", "LangShen", "AGE", "28"];
Obviously, each value or function is independent. Multiple values are separated by commas (,). Because they are array objects, they are equal:
Var LangShen = Array ("Name", "LangShen", "AGE", "28 ");
Alert (LangShen [0]);
3. {} and [] are used together. As mentioned above, {} is an object and [] is an array. We can form an array of objects, for example:
Copy codeThe Code is as follows:
Var LangShen = {"Name": "Langshen ",
"MyWife": ["LuLu", "26"],
"MySon": [{"Name": "Son1" },{ "Name": "Son2" },{ "Name": "Son3"}]
}

From the above structure, the first attribute in an object is an attribute, the second is an array, and the third is an array containing multiple objects. Calling is also a layer-by-layer access. Object Attributes are superimposed with. (points), and arrays are accessed with [subscript.
For example, alert (LangShen. MySon [1]. Name );

Related Article

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.