Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // detect whether __proto__ is supported in this browser
- var __proto_supported__ = false;
- try
- {
- __proto_supported__ = ({}).__proto__;
- } catch (e) { /* failed, so stays false */ }
- if (typeof Object.create !== 'function')
- {
- Object.create = function (o)
- {
- // this could be further modified to support ECMAScript 5 strict mode:
- function F() { if (!__proto_supported__) { this.__proto__ = arguments.callee.prototype; } }
- F.prototype = o;
- return new F();
- };
- }
- if (typeof Object.prototype.hasancestor !== 'function')
- {
- Object.prototype.hasancestor = function(o)
- {
- if (this === o) { return true; }
- else if (!this.__proto__ || !o) { return false; }
- return this.__proto__.hasancestor(o);
- };
- }
- // quick test
- var test_ancestors = function()
- {
- var x = {'a':'b'}
- var y = Object.create(x)
- if (!y.hasancestor(x)) { alert('failed'); }
- if (!y.hasancestor(y)) { alert('failed'); }
- if (y.hasancestor(new Object())) { alert('failed'); }
- var z = {'a':'b'}
- if (y.hasancestor(z)) { alert('failed'); }
- if (z.hasancestor(y)) { alert('failed'); }
- };
Add Comment
Please, Sign In to add comment