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

Let you understand the difference between [], {}, () in js (detailed explanation)

烟雨青岚
Release: 2020-07-13 11:22:49
forward
2485 people have browsed it

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”};
Copy after login

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()
Copy after login

2. [ ] brackets represent an array, which can also be understood as an array object

如:var LangShen = [ "Name","LangShen","AGE","28" ];
Copy after login

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" );
Copy after login

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"}] 
}
Copy after login

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”}
];
Copy after login

This is an object array

4. () represents multiple parameters, {} represents the function body

function show(name){
    Alert(name);
}
es6中,
let show=(name)=>{
    alert(name);
}
Copy after login

In es6, if there is only one parameter or only one function body, you can omit () and {}, such as

let show=name=>alert(name);
Copy after login

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!

Related labels:
js
source:csdn.net
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