_set-proto.js 906 B

12345678910111213141516171819202122232425
  1. // Works with __proto__ only. Old v8 can't work with null proto objects.
  2. /* eslint-disable no-proto */
  3. var isObject = require('./_is-object');
  4. var anObject = require('./_an-object');
  5. var check = function (O, proto) {
  6. anObject(O);
  7. if (!isObject(proto) && proto !== null) throw TypeError(proto + ": can't set as prototype!");
  8. };
  9. module.exports = {
  10. set: Object.setPrototypeOf || ('__proto__' in {} ? // eslint-disable-line
  11. function (test, buggy, set) {
  12. try {
  13. set = require('./_ctx')(Function.call, require('./_object-gopd').f(Object.prototype, '__proto__').set, 2);
  14. set(test, []);
  15. buggy = !(test instanceof Array);
  16. } catch (e) { buggy = true; }
  17. return function setPrototypeOf(O, proto) {
  18. check(O, proto);
  19. if (buggy) O.__proto__ = proto;
  20. else set(O, proto);
  21. return O;
  22. };
  23. }({}, false) : undefined),
  24. check: check
  25. };