false.toString(); // 'false'[1, 2, 3].toString(); // '1,2,3'function Foo(){}Foo.bar = 1;Foo.bar; // 1One thing that is often misunderstood is that numeric constants cannot be regarded as objects, but in fact numeric constants can still be regarded as objects. This is because the Javascript parser makes a mistake when parsing point operators and treats them as floating point features.
2.toString(); // raises SyntaxError
In fact, we have many ways to make a numeric 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, and they mainly contain the corresponding relationship between keys and values.
Use the {} symbol to create a simple object. This newly created object will inherit from Object.prototype and does not contain properties that it defines itself.
var foo = {}; // a new empty object// a new object with a 'test' property with value 12var bar = {test: 12};Access the properties of the object
We can use two ways to access Javascript objects, namely the dot operator. and the bracket operator[].
var foo = {name: 'kitten'}foo.name; // kittenfoo['name']; // kittenvar get = 'name';foo[get]; // kittenfoo.1234; // SyntaxErrorfoo['1234']; // worksThe effects of the two operators are almost the same, the only difference is that the bracket operator allows dynamic settings of properties and the attribute names can have syntax errors. (The third case in the above example has been explained)
Delete the object's properties
The only way to delete a property is to use delete, setting the property value to undefined or null just remove the value associated with the property and does not really delete the property 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.
It should be explained here that delete can only delete attributes, not variables. Therefore, when defining variables, we must develop the good habit of writing var. At any time, variables must be declared using the var keyword. Because if you do not write var, the variable will be mistaken for the global object and create a new property.
This example gives the answer quite clearly, a is a variable, and b is just a property of a global object.
Properties of named objects
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};The properties of an object can be named with normal characters or strings. Also due to a wrong design of the Javascript parser, the second representation method in the above example will throw an error in ECMAScript 5.
The reason for the error is that delete is a keyword, so a string constant must be named to adapt to the old version of Javascript parser.