Everything in Javascript can be regarded as an object, except for two special cases: 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 a digital constant cannot be regarded as an object. In fact, a digital constant can still be regarded as an object. This is because the Javascript parser sometimes treats it as a floating point character while parsing the vertex operator.
2. toString (); // raises SyntaxError
In fact, there are many ways to make a numerical constant behave as an object.
2..toString(); // the second point is correctly recognized2 .toString(); // note the space left to the dot(2).toString(); // 2 is evaluated first
Object as Data Type
Objects in Javascript can be used as hash tables. They mainly contain the correspondence between keys and values.
Use the {} symbol to create a simple Object. The new Object will be inherited from Object. prototype and does not contain its own defined attributes.
var foo = {}; // a new empty object// a new object with a 'test' property with value 12var bar = {test: 12};
Access Object Attributes
We can use two methods to access Javascript objects: vertex operator. and brackets operator [].
var foo = {name: 'kitten'}foo.name; // kittenfoo['name']; // kittenvar get = 'name';foo[get]; // kittenfoo.1234; // SyntaxErrorfoo['1234']; // works
The two operators have almost the same effect. The only difference is that the brackets operator allows dynamic attribute setting and the attribute name can have syntax errors. (The third case in the preceding example has been described)
Delete Object Attributes
The only way to delete an attribute is to use delete. Setting the attribute value to undefined or null only removes the attribute-related value and does not actually delete the attribute itself.
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 bar undefined and foo null, only baz is actually deleted.
Here, we must note that delete can only delete attributes and cannot delete variables. Therefore, when defining variables, we must develop a good habit of writing var. At any time, variables must use the var keyword before they can be declared. If you do not write var, the variable is mistakenly recognized as creating a new attribute for the global object.
This example clearly gives the answer: a is a variable, and B is just a Global Object attribute.
Attributes of the named object
var test = { 'case': 'I am a keyword, so I must be notated as a string', delete: 'I am a keyword, so me too' // raises SyntaxError};
Object Attributes can be named by common characters or strings. This is also because of an incorrect design of the Javascript parser. The second Representation Method in the previous example will throw an error in ECMAScript 5.
The cause of the error is that delete is a keyword, so a String constant must be used for naming to adapt to the old version of Javascript parser.