This article describes how to use javascript objects and how to operate attributes. For more information, see if all the variables in JavaScript are objects, except for null and undefined.
The Code is as follows:
False. toString (); // 'false'
[1, 2, 3]. toString (); // '1, 2, 3'
Function Foo (){}
Foo. bar = 1;
Foo. bar; // 1
A common misunderstanding is that the literal value of a number (literal) is not an object. This is because of an error in the JavaScript parser, which tries to resolve the vertex operator to a part of the floating-point numeric nominal value.
The Code is as follows:
2. toString (); // error: SyntaxError
There are many workways to make the literal value of A number look like an object.
The Code is as follows:
2. toString (); // The second vertex can be parsed normally.
2. toString (); // note the space before the dot
(2). toString (); // 2 is calculated first
Object as Data Type
JavaScript objects can be used as hash tables. They are mainly used to store the correspondence between named keys and values.
You can use the literal syntax-{}-of an object to create a simple object. This newly created Object inherits from Object. prototype and does not have any custom attributes.
The Code is as follows:
Var foo = {}; // an empty object
// A new object with a custom property 'test' with a value of 12'
Var bar = {test: 12 };
Access attributes
There are two ways to access the attributes of an object, the dot operator or the brackets operator.
The Code is as follows:
Var foo = {name: 'kitten '}
Foo. name; // kitten
Foo ['name']; // kitten
Var get = 'name ';
Foo [get]; // kitten
Foo.1234; // SyntaxError
Foo ['20140901']; // works
The two syntaxes are equivalent, but the brackets operator is still valid in the following two cases-dynamic attribute setting-attribute name is not a valid variable name (note: for example, the attribute name contains spaces or the attribute name is a JS keyword)
Note: In JSLint syntax detection tools, point operators are recommended.
Delete attributes
The only way to delete an attribute is to use the delete operator. Setting an attribute to undefined or null does not actually delete an attribute, but simply removes the association between attributes and values.
The Code is as follows:
14
Var obj = {
Bar: 1,
Foo: 2,
Baz: 3
};
Obj. bar = undefined;
Obj. foo = null;
Delete obj. baz;
For (var I in obj ){
If (obj. hasOwnProperty (I )){
Console. log (I, ''+ obj [I]);
}
}
The above output results include bar undefined and foo null-only baz is actually deleted, so it disappears from the output results.
Attribute name syntax
The Code is as follows:
Var test = {
'Case': 'I am a keyword so I must be notated as a string ',
Delete: 'I am a keyword too so me' // error: SyntaxError
};
Object attribute names can be declared using strings or common characters. However, due to another incorrect design of the JavaScript parser, the above second declaration method will throw the SyntaxError error before ECMAScript 5.
The cause of this error is that delete is a keyword of the JavaScript language. Therefore, to run normally in a lower version of the JavaScript engine, the string literal value declaration method must be used.