All variables in JavaScript are objects, with two exceptions null and undefined.
The code copy is as follows:
false.toString(); // 'false'
[1, 2, 3].toString(); // '1,2,3'
function Foo(){}
Foo.bar = 1;
Foo.bar; // 1
A common misconception is that the literal value of a number is not an object. This is because of a bug in the JavaScript parser that tries to parse the point operator as part of the floating point numerical face value.
The code copy is as follows:
2.toString(); // Error: SyntaxError
There are many workarounds to make the literal value of a number look like an object.
The code copy is as follows:
2..toString(); // The second dot can be parsed normally
2.toString(); // Pay attention to the spaces before the dot
(2).toString(); // 2 is calculated first
Object as data type
JavaScript objects can be used as hash tables and are mainly used to save the correspondence between named keys and values.
Using the literal syntax of an object - {} - can create a simple object. This newly created object inherits from Object.prototype below, without any custom properties.
The code copy 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 properties
There are two ways to access the object's properties, the dot operator or the bracket operator.
The code copy is as follows:
var foo = {name: 'kitten'}
foo.name; // kitten
foo['name']; // kitten
var get = 'name';
foo[get]; // kitten
foo.1234; // SyntaxError
foo['1234']; // works
The two syntaxes are equivalent, but the bracket operator is still valid in the following two situations - dynamically setting properties - The attribute name is not a valid variable name (Translator's note: For example, the attribute name contains spaces, or the attribute name is Keywords of JS)
Translator's note: In the JSLint syntax detection tool, point operators are recommended.
Delete attributes
The only way to delete a property is to use the delete operator; setting the property to undefined or null does not really delete the property, but simply removes the association between the property and the value.
The code copy 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 have bar undefined and foo null - only baz is actually deleted, so it disappears from the output results.
The syntax of attribute names
The code copy 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
};
The attribute name of an object can be declared using a string or a normal character. However, due to another misdesign of the JavaScript parser, the second declaration method above will throw a SyntaxError error before ECMAScript 5.
The reason for this error is that delete is a keyword in the JavaScript language; therefore, in order to run normally under the lower version of JavaScript engine, string literals must be declared using the string literal value.