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

Object.setPrototypeOf() - 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.setPrototypeOf()

Baseline Widely available

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

: [[Prototype]] , JavaScript, , JavaScript. , , Object.setPrototypeOf(), , , [[Prototype]] . , [[Prototype]] . [[Prototype]], Object.create().

In this article

Object.setPrototypeOf() ( , [[Prototype]]) null.

Object.setPrototypeOf(obj, prototype);

obj

, .

prototype

( null).

TypeError, , [[Prototype]] , Object.isExtensible(). , prototype null ( , , , undefined). [[Prototype]] obj .

js
var dict = Object.setPrototypeOf({}, null);

Object.prototype.__proto__, Object.setPrototypeOf(), :

js
if (!Object.setPrototypeOf) {
  Object.prototype.setPrototypeOf = function (obj, proto) {
    if (obj.__proto__) {
      obj.__proto__ = proto;
      return obj;
    } else {
      //       Object.create(null) 
      var Fn = function () {
        for (var key in obj) {
          //       
          Object.defineProperty(this, key, {
            value: obj[key],
          });
        }
      };
      Fn.prototype = proto;
      return new Fn();
    }
  };
}

Object.getPrototypeOf() Object.prototype.__proto__ :

js
/**
 *** Object.appendChain(@object, @prototype)
 *
 *        .
 *  @object (    ,     ).
 *
 *** Object.appendChain(@object [, "@arg_name_1", "@arg_name_2", "@arg_name_3", "..."], "@function_body")
 *** Object.appendChain(@object [, "@arg_name_1, @arg_name_2, @arg_name_3, ..."], "@function_body")
 *
 *          Function.prototype,  
 * new Function(["@arg"(s)], "@function_body")   .
 *  .
 *
 **/

Object.appendChain = function (oChain, oProto) {
  if (arguments.length < 2) {
    throw new TypeError("Object.appendChain - Not enough arguments");
  }
  if (typeof oProto === "number" || typeof oProto === "boolean") {
    throw new TypeError(
      "second argument to Object.appendChain must be an object or a string",
    );
  }

  var oNewProto = oProto,
    oReturn =
      (o2nd =
      oLast =
        oChain instanceof this ? oChain : new oChain.constructor(oChain));

  for (
    var o1st = this.getPrototypeOf(o2nd);
    o1st !== Object.prototype && o1st !== Function.prototype;
    o1st = this.getPrototypeOf(o2nd)
  ) {
    o2nd = o1st;
  }

  if (oProto.constructor === String) {
    oNewProto = Function.prototype;
    oReturn = Function.apply(null, Array.prototype.slice.call(arguments, 1));
    this.setPrototypeOf(oReturn, oLast);
  }

  this.setPrototypeOf(o2nd, oNewProto);
  return oReturn;
};

:

js
function Mammal() {
  this.isMammal = "";
}

function MammalSpecies(sMammalSpecies) {
  this.species = sMammalSpecies;
}

MammalSpecies.prototype = new Mammal();
MammalSpecies.prototype.constructor = MammalSpecies;

var oCat = new MammalSpecies("Felis");

alert(oCat.isMammal); // ''

function Animal() {
  this.breathing = "";
}

Object.appendChain(oCat, new Animal());

alert(oCat.breathing); // ''

:

js
function Symbol() {
  this.isSymbol = "";
}

var nPrime = 17;

alert(typeof nPrime); // 'number'

var oPrime = Object.appendChain(nPrime, new Symbol());

alert(oPrime); // '17'
alert(oPrime.isSymbol); // ''
alert(typeof oPrime); // 'object'

: Function.prototype

js
function Person(sName) {
  this.identity = sName;
}

var george = Object.appendChain(
  new Person(""),
  'alert(", !!");',
);

alert(george.identity); // ''
george(); // ', !!'

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.setprototypeof


Web Proxy Viewer  |  New URL  |  Original Page