| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create | [Back] [Original] |
Get to know MDN better
const person = {
isHuman: false,
printIntroduction: function () {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
},
};
const me = Object.create(person);
me.name = "Matthew"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // Inherited properties can be overwritten
me.printIntroduction();
// Expected output: "My name is Matthew. Am I human? true"
Object.create(proto)
Object.create(proto, propertiesObject)
Object.create() JavaScript
// Shape
function Shape() {
this.x = 0;
this.y = 0;
}
//
Shape.prototype.move = function (x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};
// Rectangle
function Rectangle() {
Shape.call(this); //
}
//
Rectangle.prototype = Object.create(Shape.prototype, {
// Rectangle.prototype.constructor Rectangle
// Shape prototype.constructor
// prototype.constructor Rectangle
constructor: {
value: Rectangle,
enumerable: false,
writable: true,
configurable: true,
},
});
const rect = new Rectangle();
console.log("rect Rectangle ", rect instanceof Rectangle); // true
console.log("rect Shape ", rect instanceof Shape); // true
rect.move(1, 1); // 'Shape moved.'
create() constructor Object.create() Object.setPrototypeOf()
Object.create() Object.create() Object.create()
o = {};
//
o = Object.create(Object.prototype);
o = Object.create(Object.prototype, {
// foo
foo: {
writable: true,
configurable: true,
value: "hello",
},
// bar
bar: {
configurable: false,
get() {
return 10;
},
set(value) {
console.log("Setting `o.bar` to", value);
},
},
});
// 'p' 42
o = Object.create({}, { p: { value: 42 } });
Object.create() null __proto__
o = Object.create(null);
//
o = { __proto__: null };
o.p = 24; //
o.p; // 42
o.q = 12;
for (const prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false
writableenumerable configurable
o2 = Object.create(
{},
{
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true,
},
},
);
//
// o2 = Object.create({ p: 42 })
// { p: 42 }
Object.create() new
function Constructor() {}
o = new Constructor();
//
o = Object.create(Constructor.prototype);
Constructor Object.create()
| ECMAScript 2027 LanguageSpecification # sec-object.create |
ObjectObject.assign()Object.create()Object.defineProperties()Object.defineProperty()Object.entries()Object.freeze()Object.fromEntries()Object.getOwnPropertyDescriptor()Object.getOwnPropertyDescriptors()Object.getOwnPropertyNames()Object.getOwnPropertySymbols()getPrototypeOf()Object.groupBy()Object.hasOwn()is()Object.isExtensible()Object.isFrozen()Object.isSealed()Object.keys()Object.preventExtensions()Object.seal()Object.setPrototypeOf()Object.values()Object.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |