$.set-proto.js 914 B

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