Skip to content

Instantly share code, notes, and snippets.

@dmorosinotto
Created May 20, 2017 13:32
Show Gist options
  • Save dmorosinotto/47d833457b1fbe643355c5b3c9a5c14c to your computer and use it in GitHub Desktop.
Save dmorosinotto/47d833457b1fbe643355c5b3c9a5c14c to your computer and use it in GitHub Desktop.
Possible implementation of Object.implement
//CODE by Andrea Giammarchi - READ MORE: http://webreflection.blogspot.it/2009/06/wait-moment-javascript-does-support.html
Object.implement = function(o, constructor){
// Another WebReflection Insane Snippet
if(o instanceof constructor)
// nothing to check
// classical boring stuff
return true;
// let's check if things are OK
var k, b = true,
// take the instance constructor prototype
po = o.constructor.prototype,
// take the implemented prototype
pc = constructor.prototype
;
// for each property or method
// in the implemented constructor
for(k in pc)
// check if the instance inherited prototype
// has this method/property as well
b = b && k in po; //* TOTHINK: */ && (typeof po[k] === typeof pc[k]); //*/
// if there was nothing to check
// we cannot say a word ... but ...
// if instance has every method/property
// present in the compared constructor
// prototype, we could say this instance
// implements this constructor
return !!k && b;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment