A common misunderstanding is that the literal value of a number (literal) cannot be used as 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.
Object as Data Type
Access attributes
Delete attributes
Attribute name syntax
Object usage and attributes
All variables in JavaScript can be used as objects, except for null and undefined.
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) cannot be used as 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.
2. toString (); // error: SyntaxError
There are many workways to make the literal value of A number look like an object.
2 .. toString (); // The second vertex can be parsed normally. 2. toString (); // note the space before the dot (2 ). toString (); // 2 is counted 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.
Var foo = {}; // an empty object // a new object with a custom property 'test' var bar = {test: 12} with a value of 12 };
Access attributes
There are two ways to access the attributes of an object, the dot operator or the brackets operator.
var foo = {name: 'kitten'}foo.name; // kittenfoo['name']; // kittenvar get = 'name';foo[get]; // kittenfoo.1234; // SyntaxErrorfoo['1234']; // works
The two syntaxes are equivalent, but the brackets operator is still valid in the following two cases:
Dynamic attribute setting
The property name is not a valid variable name (for example, the property name contains spaces or the property name is a JS keyword)
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.
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
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.
The above is the content of the JavaScript advanced series-object usage and attributes. For more information, see PHP Chinese website (www.php1.cn )!