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

Object.create() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

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 2015 ..

Object.create() .

In this article

Object.create(proto[, propertiesObject])

proto

, .

propertiesObject

. undefined, , ( , , ) , . . Object.defineProperties().

TypeError, proto null ( - ).

: 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(" .");
};

// Rectangle  
function Rectangle() {
  Shape.call(this); //   
}

//   
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

console.log(
  "  rect  Rectangle? " + (rect instanceof Rectangle),
); // true
console.log("  rect  Shape? " + (rect instanceof Shape)); // true
rect.move(1, 1); //  ' .'

, .

js
function MyClass() {
  SuperClass.call(this);
  OtherSuperClass.call(this);
}

MyClass.prototype = Object.create(SuperClass.prototype); // 
mixin(MyClass.prototype, OtherSuperClass.prototype); // 

MyClass.prototype.myMethod = function () {
  // - 
};

, . jQuery.extend().

: propertiesObject Object.create()

js
var o;

//     
o = Object.create(null);

o = {};
//  :
o = Object.create(Object.prototype);

//         .
// ( ,       * *.)
o = Object.create(Object.prototype, {
  // foo   '-'
  foo: { writable: true, configurable: true, value: "" },
  // bar       ( )
  bar: {
    configurable: false,
    get: function () {
      return 10;
    },
    set: function (value) {
      console.log(" `o.bar` ", value);
    },
    /*     ES5      :
    get function() { return 10; },
    set function(value) { console.log(' `o.bar` ', value); } */
  },
});

function Constructor() {}
o = new Constructor();
//  :
o = Object.create(Constructor.prototype);
// ,     Constructor     ,
//   Object.create()    

//   ,      
//     'p'   42
o = Object.create({}, { p: { value: 42 } });

//      ,   :
o.p = 24;
o.p;
// 42

o.q = 12;
for (var prop in o) {
  console.log(prop);
}
// 'q'

delete o.p;
// false

//    ES3
o2 = Object.create(
  {},
  {
    p: {
      value: 42,
      writable: true,
      enumerable: true,
      configurable: true,
    },
  },
);

Object.prototype.hasOwnProperty.

js
if (typeof Object.create != "function") {
  //   ECMA-262,  5, 15.2.3.5
  // : http://es5.github.io/#x15.2.3.5
  Object.create = (function () {
    //   ,   
    function Temp() {}

    //     Object.prototype.hasOwnProperty
    var hasOwn = Object.prototype.hasOwnProperty;

    return function (O) {
      // 1.  Type(O)   Object or Null   TypeError.
      if (typeof O != "object") {
        throw TypeError("Object prototype may only be an Object or null");
      }

      // 2.  obj     ,   
      //     new Object(),  Object   
      //        
      // 3.     [[Prototype]]  obj  O.
      Temp.prototype = O;
      var obj = new Temp();
      Temp.prototype = null; //        ...

      // 4.    Properties    , 
      //       obj,     
      //     Object.defineProperties   obj 
      //    Properties.
      if (arguments.length > 1) {
        // Object.defineProperties  ToObject   .
        var Properties = Object(arguments[1]);
        for (var prop in Properties) {
          if (hasOwn.call(Properties, prop)) {
            obj[prop] = Properties[prop];
          }
        }
      }

      // 5.  obj
      return obj;
    };
  })();
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.create


Web Proxy Viewer  |  New URL  |  Original Page