27 lines
1,002 B
JavaScript
27 lines
1,002 B
JavaScript
|
/* eslint-disable no-proto -- safe */
|
||
|
var anObject = require('../internals/an-object');
|
||
|
var aPossiblePrototype = require('../internals/a-possible-prototype');
|
||
|
|
||
|
// `Object.setPrototypeOf` method
|
||
|
// https://tc39.es/ecma262/#sec-object.setprototypeof
|
||
|
// Works with __proto__ only. Old v8 can't work with null proto objects.
|
||
|
// eslint-disable-next-line es/no-object-setprototypeof -- safe
|
||
|
module.exports = Object.setPrototypeOf || ('__proto__' in {} ? function () {
|
||
|
var CORRECT_SETTER = false;
|
||
|
var test = {};
|
||
|
var setter;
|
||
|
try {
|
||
|
// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
|
||
|
setter = Object.getOwnPropertyDescriptor(Object.prototype, '__proto__').set;
|
||
|
setter.call(test, []);
|
||
|
CORRECT_SETTER = test instanceof Array;
|
||
|
} catch (error) { /* empty */ }
|
||
|
return function setPrototypeOf(O, proto) {
|
||
|
anObject(O);
|
||
|
aPossiblePrototype(proto);
|
||
|
if (CORRECT_SETTER) setter.call(O, proto);
|
||
|
else O.__proto__ = proto;
|
||
|
return O;
|
||
|
};
|
||
|
}() : undefined);
|