Guest User

Untitled

a guest
May 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. // detect whether __proto__ is supported in this browser
  2. var __proto_supported__ = false;
  3. try
  4. {
  5. __proto_supported__ = ({}).__proto__;
  6. } catch (e) { /* failed, so stays false */ }
  7.  
  8. if (typeof Object.create !== 'function')
  9. {
  10. Object.create = function (o)
  11. {
  12. // this could be further modified to support ECMAScript 5 strict mode:
  13. function F() { if (!__proto_supported__) { this.__proto__ = arguments.callee.prototype; } }
  14. F.prototype = o;
  15. return new F();
  16. };
  17. }
  18.  
  19. if (typeof Object.prototype.hasancestor !== 'function')
  20. {
  21. Object.prototype.hasancestor = function(o)
  22. {
  23. if (this === o) { return true; }
  24. else if (!this.__proto__ || !o) { return false; }
  25. return this.__proto__.hasancestor(o);
  26. };
  27. }
  28.  
  29. // quick test
  30. var test_ancestors = function()
  31. {
  32. var x = {'a':'b'}
  33. var y = Object.create(x)
  34. if (!y.hasancestor(x)) { alert('failed'); }
  35. if (!y.hasancestor(y)) { alert('failed'); }
  36. if (y.hasancestor(new Object())) { alert('failed'); }
  37. var z = {'a':'b'}
  38. if (y.hasancestor(z)) { alert('failed'); }
  39. if (z.hasancestor(y)) { alert('failed'); }
  40. };
Add Comment
Please, Sign In to add comment