Second, the object
(1) Object literal
Object literals provide a very convenient way to create new objects that deserve notation. The object literal appears where any allowed expression appears.
(2) Search
To retrieve the values in an object, you can use the [] suffix to enclose a string expression in the same way. If the string is a string literal and is a legitimate JavaScript identifier and is not a reserved word, you can also access it. Priority, within it is compact and readable.
① If you try to retrieve a value that does not exist, then return to undefined.
②| | Operator can be used to populate the default values.
var middle = stooge[' Middle_name ' | | ' Unknown ';
③ attempting to take a value from the member property of undefined will result in a typeerror error. It is possible to use && to avoid errors.
// undefined // throw "TypeError"flight.equipment && flight.equipment.model // undefined
(3) Update
The values in the object can be updated by an assignment statement. If the property name already exists in the object, then this property will be replaced.
(4) References
Objects are passed by reference. They will never be copied.
(5) prototypes
Each object is connected to a prototype object and can inherit properties from it. All objects created by object literals are connected to the Object.prototype, which is the standard object in JavaScript.
The ① prototype connection is not functional at the time of the update. When we make a change to an object, we don't touch the object's prototype.
② prototype connections are only used when the search is worth it. If we try to get a property value, but the object does not have this property name, then JavaScript tries to get the property value from the prototype object. If the prototype object does not have the attribute value, it is searched from its prototype until the final end of the Object.prototype. If the desired attribute does not yet exist, then the result is undefined. This process is called a delegate.
③ prototype relationship is a dynamic relationship. If we add a new attribute to the prototype, the property is immediately visible to all objects based on that prototype.
(6) Reflection
One method is to use the hasOwnProperty method, which returns true if the object has a unique property. This method does not check the prototype chain.
(7) enumeration
The for-in statement can be used to traverse all property names in an object. The enumeration process will list all properties-including functions and properties in prototypes that you might not care about-all of which are necessary to filter. Generally used hasOwnProperty method and typeof to deal with.
var name; for inch Anthor_stooge) { iftypeof Anthor_stooge[name]!== ' function ' ) { + ': ' + anthor_stooge[ Name]);} }
The order in which property names appear is not deterministic, so be prepared for any order that might occur. If you want to make sure that the order is specific, then using an array is the best option.
(8) Delete
The delete operator can be used to delete an object's properties. If the object contains this property, the property is deleted, but it does not touch any objects in the prototype chain.
(9) Reduce global variables
One way to minimize global variables is to create a unique global variable for your app.
The essence of JavaScript language--Object article