首页 > 代码库 > Object
Object
An object is an unordered collection of properties, each of witch has a name and value.
An object is more than a simple string-to-value map, in addition to maintaining its own set of properties.
a JavaScript object alse inherits the properties of another object, know as its "prototype", and this "prototypal inheritance" is a key feature of JavaScript
JavaScript objects are dynamic--properties can usually be added and deleted.
Object can be created with object literals, with the new keyword, and with the Object.create() function.
var empty = {}; // An object with no propertiesvar point = { x:0, y:0 }; // Two propertiesvar point2 = { x:point.x, y:point.y+1 }; // More complex valuesvar book = { "main title": "JavaScript", // Property names include spaces, ‘sub-title‘: "The Definitive Guide", // and hyphens, so use string literals "for": "all audiences", // for is a reserved word, so quote author: { // The value of this property is firstname: "David", // itself an object. Note that surname: "Flanagan" // these property names are unquoted. }};
var o = new Object(); // Create an empty object: same as {}.var a = new Array(); // Create an empty array: same as [].var d = new Date(); // Create a Date object representing the current timevar r = new RegExp("js"); // Create a RegExp object for pattern matching.
Every Javascript object has a secend object associated with it. This second object is known as a prototype, and the first object inherits properties from the prototype.
All objects created by object literals have the same prototype object, and we can refer to this object like this:
Object.prototype.
Object created using the new keyword and a constructor invocation use the value of the prototype property of the constructor functions as their prototype.
So the object created by new Object() inherit from Object.prototype just as the object created by {} dose.
Object.prototype is one of the rare object that has no prototype: it does not inherit any properties.
All of the built-in constructors have a prototype that inherit from Object.prototype.
Object.create() creates a new object, using its first argument as the prototype of the object
var o1 = Object.create({x:1, y:2}); // o1 inherits properties x and y.
Object