Home  >  Article  >  Web Front-end  >  A brief discussion on js literals, access to object literals, and the usage of the keyword in

A brief discussion on js literals, access to object literals, and the usage of the keyword in

高洛峰
高洛峰Original
2016-12-06 13:38:311048browse

1: Literal meaning

Literal represents how to express the value. Generally, except for expressions, when assigning a value to a variable, the right side of the equal sign can be considered a literal.

Literals are divided into string literals, array literals and

object literals, in addition to function literals.

Example:

var test="hello world!";
"hello world!" is a string literal, and test is a variable name.

2: Object literals

Object literals have two access methods: the example is as follows,

var obj = {
a:'aaa', //a is an attribute, 'aaa' is an attribute value
b:' bbb',
c:'ccc'
}

Method one: obj.a// aaa, this method is invalid when for in traverses the object...

Method two: obj['a']//aaa, Quotation marks are required

[The dot method is only suitable when the attribute is a string. If the attribute is a variable, only the latter can be used]

When the attribute is a variable, the value can only be assigned using the following method:


var obj = {};
obj[$a] = 'value';

If you write {$a: 'value'} directly, $a will be parsed into a string.

Three: Usage of keyword in

Format: (variable in object)... Note,,,

When the "object" is an array, the "variable" refers to the "index" of the array;

When "object" is an object, "variable" refers to the "property" of the object.


Statement:
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