Creates an object that has the specified prototype, and that optionally contains specified properties.
SyntaxObject.create( prototype , descriptors )
A new object that has the specified internal prototype and contains the specified properties, if any.
ExamplesThe following example creates an object using a null prototype and adds two enumerable properties.
var newObj = Object.create(null, {
size: {
value: "large",
enumerable: true
},
shape: {
value: "round",
enumerable: true
}
});
document.write(newObj.size + "<br/>");
document.write(newObj.shape + "<br/>");
document.write(Object.getPrototypeOf(newObj));
The following example creates an object that has the same internal prototype as the Object object. You can see that it has the same prototype as an object created by using an object literal. The Object.getPrototypeOf function gets the prototype of the original object. To get the object’s property descriptor, you can use Object.getOwnPropertyDescriptor Function.
var firstLine = { x: undefined, y: undefined };
var secondLine = Object.create(Object.prototype, {
x: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
},
y: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
}
});
var firstLine = { x: undefined, y: undefined };
var secondLine = Object.create(Object.prototype, {
x: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
},
y: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
}
});
document.write("first line prototype = " + Object.getPrototypeOf(firstLine));
document.write("<br/>");
document.write("second line prototype = " + Object.getPrototypeOf(secondLine));
The following example creates an object that has the same internal prototype as the Shape object.
var Shape = { twoDimensional: true, color: undefined, hasLineSegments: undefined };
var Square = Object.create(Object.getPrototypeOf(Shape));
Remarks
You can use this function using a null prototype parameter in order to stop the prototype chain. The object created will have no prototype.
ExceptionsA TypeError exception is thrown if any of the following conditions is true:
Microsoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4