Signatur
Object.create(proto) / Object.create(proto, propertiesObject)
Beschreibung
Die statische Methode Object.create() erzeugt ein neues Objekt und verwendet ein bestehendes Objekt als Prototype des neu erzeugten Objekts.
Parameter
| Name | Typ | Default | Beschreibung |
|---|---|---|---|
| $proto Pflicht | Object | null | — | Das Objekt, das als Prototype des neu erzeugten Objekts dienen soll. |
| $propertiesObject | Object | — | Falls angegeben und nicht undefined, ein Objekt, dessen enumerable eigene Properties Property-Deskriptoren angeben, die dem neu erzeugten Objekt unter den entsprechenden Property-Namen hinzugefügt werden. Diese Properties entsprechen dem zweiten Argument von Object.defineProperties(). |
Rückgabewert
Typ
Object
Beschreibung
Ein neues Objekt mit dem angegebenen Prototype-Objekt und den angegebenen Properties.
Beispiele
Klassische Vererbung mit Object.create()
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function (x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype, {
// If you don't set Rectangle.prototype.constructor to Rectangle,
// it will take the prototype.constructor of Shape (parent).
// To avoid that, we set the prototype.constructor to Rectangle (child).
constructor: {
value: Rectangle,
enumerable: false,
writable: true,
configurable: true,
},
});
const rect = new Rectangle();
console.log("Is rect an instance of Rectangle?", rect instanceof Rectangle); // true
console.log("Is rect an instance of Shape?", rect instanceof Shape); // true
rect.move(1, 1); // Logs 'Shape moved.'
Verwendung des propertiesObject-Arguments mit Object.create()
o = {};
// Is equivalent to:
o = Object.create(Object.prototype);
o = Object.create(Object.prototype, {
// foo is a regular data property
foo: {
writable: true,
configurable: true,
value: "hello",
},
// bar is an accessor property
bar: {
configurable: false,
get() {
return 10;
},
set(value) {
console.log("Setting `o.bar` to", value);
},
},
});
// Create a new object whose prototype is a new, empty
// object and add a single property 'p', with value 42.
o = Object.create({}, { p: { value: 42 } });
Objekt mit null als Prototype
o = Object.create(null);
// Is equivalent to:
o = { __proto__: null };
Standardmäßig sind Properties nicht writable, enumerable oder configurable
o.p = 24; // throws in strict mode
o.p; // 42
o.q = 12;
for (const prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false; throws in strict mode
Property mit denselben Attributen wie in einem Initializer angeben
o2 = Object.create(
{},
{
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true,
},
},
);
// This is not equivalent to:
// o2 = Object.create({ p: 42 })
// which will create an object with prototype { p: 42 }
Verhalten des new-Operators nachbilden
function Constructor() {}
o = new Constructor();
// Is equivalent to:
o = Object.create(Constructor.prototype);
// Wichtig · Fallstricke
Wirft einen
TypeError, wenn proto weder null noch ein Object ist.