HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
Esta página ha sido traducida del inglés por la comunidad. Aprende más y únete a la comunidad de MDN Web Docs.
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since septiembre de 2015.
El método Object.setPrototypeOf() establece el prototipo (p.e., la propiedad interna [[Prototype]]) de un objeto especificado a otro objeto o sino establece null.
Object.setPrototypeOf()
[[Prototype]]
null
Advertencia: Adverdencia: Cambiar la propiedad [[Prototype]] de un objeto, debido a la naturaleza de la optimización del acceso a propiedades de los motores modernos de JavaScript, es una operación bastante lenta, en todo todo navegador y motor de JavaScript. Los efectos sobre el rendimiento al alterar la herencia son sutiles y vastos., y no están limitados a simplemente el tiempo gastado en la sentencia obj.__proto___ = ..., but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().
obj.__proto___ = ...
Object.create()
Object.setPrototypeOf(obj, prototype);
obj
El objeto al que se ha de establecer el prototipo.
prototype
El nuevo prototipo del objeto, (un objeto o null).
Arroja una excepción del tipo TypeError si el objeto cuyo [[Prototype]] se va a modificar no es extensible de acuerdo con Object.isExtensible(). No hace nada si el parametro prototype no es un objeto o null (p.e., número, cadena, booleano, o undefined). De cualquier otra forma, este método cambia la propiedad [[Prototype]] del obj al valor nuevo.
TypeError
Object.isExtensible()
undefined
Object.setPrototypeOf() está en el último borrador del estandar ECMAScript6. Es considerado generalmente la manera adecuada de establecer el prototipo de un objeto, contra la propiedad más controversial Object.prototype.__proto__.
Object.prototype.__proto__
var dict = Object.setPrototypeOf({}, null);
Utilizando la vieja propiedad Object.prototype.__proto__, podemos definir facilmente Object.setPrototypeOf si aún no está disponible:
Object.setPrototypeOf
// Solo funciona en Chrome y FirefoxOnly works in Chrome y FireFox, no funciona en IE: Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) { obj.__proto__ = proto; return obj; };
Una combinación de Object.getPrototypeOf() y Object.prototype.__proto__ permite agregar una nueva cadena de prototipos al nuevo prototipo del objeto.
Object.getPrototypeOf()
/** *** Object.appendChain(@object, @prototype) * * Agrega el primer prototipo no-nativo de una cadena a un nuevo prototipo. * Retorna @object (si es Primitivo (Primitive value) será transoformado a Objeto). * *** 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") * * Agrega el primer prototipo no-nativo de una cadena a la Function.prototype nativa del objeto, luego agrega una * ueva Function(["@arg"(s)], "@function_body") a la cadena. * Retorna la función. * **/ 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; };
function Mammal() { this.isMammal = "yes"; } function MammalSpecies(sMammalSpecies) { this.species = sMammalSpecies; } MammalSpecies.prototype = new Mammal(); MammalSpecies.prototype.constructor = MammalSpecies; var oCat = new MammalSpecies("Felis"); console.log(oCat.isMammal); // 'yes' function Animal() { this.breathing = "yes"; } Object.appendChain(oCat, new Animal()); console.log(oCat.breathing); // 'yes'
function Symbol() { this.isSymbol = "yes"; } var nPrime = 17; console.log(typeof nPrime); // 'number' var oPrime = Object.appendChain(nPrime, new Symbol()); console.log(oPrime); // '17' console.log(oPrime.isSymbol); // 'yes' console.log(typeof oPrime); // 'object'
function Person(sName) { this.identity = sName; } var george = Object.appendChain( new Person("George"), 'console.log("Hello guys!!");', ); console.log(george.identity); // 'George' george(); // 'Hello guys!!'
Enable JavaScript to view this browser compatibility table.
Reflect.setPrototypeOf()
Object.prototype.isPrototypeOf()