Home >Web Front-end >JS Tutorial >Let you understand the difference between [], {}, () in js (detailed explanation)
1. { } curly brackets indicate the definition of an object. In most cases, there must be pairs of attributes and values, or a function body
{} represents the object, [] represents the properties and methods of the object, () if used after the method name, means calling
For example:
var LangShen = {"Name":"Langshen","AGE":”28”};
The above declares a file named " "LangShen" object, multiple properties or functions are separated by, (comma), because they are properties of the object,
, so when accessing, you should use . (dot) to access layer by layer: LangShen.Name, LangShen .AGE,
Of course, we can also access it in array, such as: LangShen["Name"], LangShen["AGE"], the result is the same.
var LangShen = { Name : function(){ return "LangShen"; }, Age : function(){ return "28"; } } 调用 LangShen.Name()
2. [ ] brackets represent an array, which can also be understood as an array object
如:var LangShen = [ "Name","LangShen","AGE","28" ];
Obviously, each value or function is independent , multiple values are only separated by, (comma), because it is an array object, so it is equal to:
var LangShen = Array( "Name","LangShen","AGE","28" );
When accessed, it is the same as the 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
var LangShen = { "Name":"Langshen", "MyWife":[ "LuLu","26" ], "MySon":[{"Name":"Son1"},{"Name":"Son2"},{"Name":"Son3"}] }
From Judging from the above structure, the first item in an object is an attribute, the second item is an array, and the third item 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 ) ; var LangShen=[ {“name”:“wangwu”}, {“name”:“lieu”} ];
This is an object array
4. () represents multiple parameters, {} represents the function body
function show(name){ Alert(name); } es6中, let show=(name)=>{ alert(name); }
In es6, if there is only one parameter or only one function body, you can omit () and {}, such as
let show=name=>alert(name);
Thank you for reading, I hope you will benefit.
This article is reproduced from: https://blog.csdn.net/qq_20069429/article/details/83267887
Recommended tutorial: "JS Tutorial"
The above is the detailed content of Let you understand the difference between [], {}, () in js (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!