Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.create()

BaselineWidely available

TheObject.create() static method creates a new object, using an existing object as the prototype of the newly created object.

Try it

const person = {  isHuman: false,  printIntroduction() {    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 overwrittenme.printIntroduction();// Expected output: "My name is Matthew. Am I human? true"

Syntax

js
Object.create(proto)Object.create(proto, propertiesObject)

Parameters

proto

The object which should be the prototype of the newly-created object.

propertiesObjectOptional

If specified and notundefined, an object whoseenumerable own properties specify property descriptors to be added to the newly-created object, with the corresponding property names. These properties correspond to the second argument ofObject.defineProperties().

Return value

A new object with the specified prototype object and properties.

Exceptions

TypeError

Thrown ifproto is neithernull nor anObject.

Examples

Classical inheritance with Object.create()

Below is an example of how to useObject.create() to achieve classical inheritance. This is for a single inheritance, which is all that JavaScript supports.

js
// Shape - superclassfunction Shape() {  this.x = 0;  this.y = 0;}// superclass methodShape.prototype.move = function (x, y) {  this.x += x;  this.y += y;  console.info("Shape moved.");};// Rectangle - subclassfunction Rectangle() {  Shape.call(this); // call super constructor.}// subclass extends superclassRectangle.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); // trueconsole.log("Is rect an instance of Shape?", rect instanceof Shape); // truerect.move(1, 1); // Logs 'Shape moved.'

Note that there are caveats to watch out for usingcreate(), such as re-adding theconstructor property to ensure proper semantics. AlthoughObject.create() is believed to have better performance than mutating the prototype withObject.setPrototypeOf(), the difference is in fact negligible if no instances have been created and property accesses haven't been optimized yet. In modern code, theclass syntax should be preferred in any case.

Using propertiesObject argument with Object.create()

Object.create() allows fine-tuned control over the object creation process. Theobject initializer syntax is, in fact, a syntax sugar ofObject.create(). WithObject.create(), we can create objects with a designated prototype and also some properties. Note that the second parameter maps keys toproperty descriptors — this means you can control each property's enumerability, configurability, etc. as well, which you can't do in object initializers.

js
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 } });

WithObject.create(), we can create an objectwithnull as prototype. The equivalent syntax in object initializers would be the__proto__ key.

js
o = Object.create(null);// Is equivalent to:o = { __proto__: null };

By default properties arenot writable, enumerable or configurable.

js
o.p = 24; // throws in strict modeo.p; // 42o.q = 12;for (const prop in o) {  console.log(prop);}// 'q'delete o.p;// false; throws in strict mode

To specify a property with the same attributes as in an initializer, explicitly specifywritable,enumerable andconfigurable.

js
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 }

You can useObject.create() to mimic the behavior of thenew operator.

js
function Constructor() {}o = new Constructor();// Is equivalent to:o = Object.create(Constructor.prototype);

Of course, if there is actual initialization code in theConstructor function, theObject.create() method cannot reflect it.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.create

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp