[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/create [Back]  [Original]

Object.create() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.create()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

Object.create()

In this article

js
Object.create(proto[, propertiesObject])

proto

(prototype)

propertiesObject

undefined ( enumerable ) (property descriptors) Object.defineProperties()

proto null TypeError

Object.create()

Object.create() JavaScript .

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

js
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

js
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

polyfill

ES5 Object.create [[Prototype]] null ECMAScript 5 polyfill limitation inherent

js
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


Web Proxy Viewer  |  New URL  |  Original Page