| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/create | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.
Object.create()
Object.create(proto[, propertiesObject])
proto(prototype)
propertiesObject undefined ( enumerable ) (property descriptors) Object.defineProperties()
Object.create() 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); // call super constructor.
}
// (extends)
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var 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); // Outputs, 'Shape moved.'
mixin
function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
}
//
MyClass.prototype = Object.create(SuperClass.prototype);
// mixin
Object.assign(MyClass.prototype, OtherSuperClass.prototype);
//
MyClass.prototype.constructor = MyClass;
MyClass.prototype.myMethod = function () {
// do a thing
};
Object.assign OtherSuperClass MyClass MyClass Object.assign ES2015 polyfill jQuery.extend() .assign()
propertiesObject var o;
// null
o = Object.create(null);
o = {};
// :
o = Object.create(Object.prototype);
// Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
// foo
foo: { writable: true, configurable: true, value: "hello" },
// bar getter-and-setter
bar: {
configurable: false,
get: function () {
return 10;
},
set: function (value) {
console.log("Setting `o.bar` to", value);
},
/* with ES5 Accessors our code can look like this
get function() { return 10; },
set function(value) { console.log('setting `o.bar` to', value); } */
},
});
function Constructor() {}
o = new Constructor();
// :
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the
// Constructor function, the Object.create() cannot reflect it
// 42 'p'
o = Object.create({}, { p: { value: 42 } });
// writable, enumerable , configurable false
o.p = 24;
o.p;
// 42
o.q = 12;
for (var prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false
// to specify an ES3 property
o2 = Object.create(
{},
{
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true,
},
},
);
polyfill
ES5 Object.create [[Prototype]] null ECMAScript 5 polyfill limitation inherent
if (typeof Object.create !== "function") {
Object.create = function (proto, propertiesObject) {
if (!(
proto === null ||
typeof proto === "object" ||
typeof proto === "function"
)) {
throw TypeError("Argument must be an object, or null");
}
var temp = new Object();
temp.__proto__ = proto;
if (typeof propertiesObject === "object")
Object.defineProperties(temp, propertiesObject);
return temp;
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-object.create |
Object.defineProperty()Object.defineProperties()Object.prototype.isPrototypeOf()This page was last modified on 2026630 by MDN contributors.
ObjectObject.assign()Object.create()defineProperties()Object.defineProperty()Object.entries()Object.freeze()fromEntries()getOwnPropertyDescriptor()getOwnPropertyDescriptors()getOwnPropertyNames()getOwnPropertySymbols()Object.getPrototypeOf()groupBy()hasOwn()is()isExtensible()isFrozen()isSealed()Object.keys()Object.preventExtensions()seal()setPrototypeOf()values()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |