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

What are initialization expressions for javascript objects and arrays?

伊谢尔伦
Release: 2017-07-19 09:50:49
Original
2185 people have browsed it

Object and array initialization are actually newly created objects and arrays. These initialization expressions are sometimes called "object literals" and "array literals". However, unlike Boolean literals, they are not primitive expressions because the members or elements they contain are subexpressions.

The syntax of the initialization expression of the array is very simple. Let’s start with the following.

The initialization expression of the array is composed of a pair of square brackets and a list separated by commas. The initialization result is a Newly created array. The elements of the array are the values ​​of a comma-separated expression.

[] //An empty array; leaving empty within [] means that the array does not have any elements
[1+2,3+4] //An array with two elements, the first one 3. The second one is 7
The element initialization expression in the array initialization expression can be an array initialization expression. That is to say, expressions can be nested

var mat = [[1,2,3],[4,5,6],[7,8,9]];
Copy after login

The elements between the lists in the array literal can be omitted, and the gaps will be filled with undefined. For example, as follows:

var a=[1,,,,5]
Copy after login

The 4 elements are undefined. Leaving a comma at the end of the array literal will not create a new element with a value of undefined.

Object initialization expressions are very similar to array initialization expressions, except that the square brackets are replaced by curly braces. And each sub-expression contains a property name and a non-colon as a prefix.

var p = {x: 2.1,y: -3} //一个拥有两个属性成员的对象
   var q = {}; //空对象
   q.x=2.1;q.y=-3;  //q的属性成员和p的一样
Copy after login

Object literals can also be nested. For example,

var anh = {left:{x:2,y:3},
    right:{x:4,y:5}}
Copy after login

When JavaScript calculates the value of an object initialization expression, the object expressions will be calculated once each, and they do not have to contain constant values: they Can be any javascript expression. Likewise, the names of properties in object literals can be strings rather than identifiers. (Very useful when only reserved words or some illegal identifiers can be used as attribute names in that line)

var side = 1;
var square = {"left":{x:p.x,y:p.y},
'right':{x:p.x+side,y:p.y+side}}
Copy after login

The above is the detailed content of What are initialization expressions for javascript objects and arrays?. For more information, please follow other related articles on the PHP Chinese website!

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