Home >Web Front-end >JS Tutorial >A brief discussion on js literals, access to object literals, and the usage of the keyword in
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.