Home > Web Front-end > JS Tutorial > Detailed explanation of the use of {}, [] square brackets and curly brackets in js_javascript skills

Detailed explanation of the use of {}, [] square brackets and curly brackets in js_javascript skills

WBOY
Release: 2016-05-16 18:06:47
Original
1052 people have browsed it
1. { } curly brackets indicate the definition of an object. In most cases, it must have paired attributes and values, or functions.
For example: var LangShen = {"Name":"Langshen","AGE":"28"};
The above declares an object named "LangShen", multiple properties or functions are , (comma) separated, because it is an attribute of the object,
, so when accessing, you should use . (dot) to access layer by layer: LangShen.Name, LangShen.AGE. Of course, we can also use arrays to access, For example: LangShen["Name"], LangShen["AGE"], the results are the same.
This writing method is often used in JSON data structures. In addition, we often use it when we write function groups, such as:
Copy Code The code is as follows:

var LangShen = {
Name = function(){
return "LangShen";
},
Age = function(){
return "28";
}
}

The calling method is similar. Because it is a function group, () must be added, such as: alert( LangShen.Name() );
2. [ ] brackets represent 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. Only separated by, (comma), because it is an array object, so it is equal to:
var LangShen = Array( "Name", "LangShen", "AGE", "28" );
When accessing, It is also the same as an array, alert( LangShen[0] );
3. { } and [ ] are used together. As we mentioned earlier, { } is an object and [ ] is an array. We can form an Object array, such as:
Copy code The code is as follows:

var LangShen = { "Name":"Langshen",
"MyWife":[ "LuLu","26" ],
"MySon":[{"Name":"Son1"},{"Name":"Son2 "},{"Name":"Son3"}]
}

From the above structure, the first item in an object is an attribute, and the second item is an array , and the third one is an array containing multiple objects. When called, it is also accessed layer by layer. The properties of the object are superimposed with . (dot), and the array is accessed with [subscript].
For example: alert(LangShen.MySon[1].Name);
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