Skip to content

Instantly share code, notes, and snippets.

@fabriceleal
Last active December 18, 2015 11:38
Show Gist options
  • Save fabriceleal/5776544 to your computer and use it in GitHub Desktop.
Save fabriceleal/5776544 to your computer and use it in GitHub Desktop.
Polymorphism in Javascript
Object.prototype.test = function(){ console.log('an object')};
Array.prototype.test = function(){ console.log('an array')};
Number.prototype.test = function(){ console.log('a number')};
var a = {};
a.test(); // prints "an object"
a = [];
a.test(); // prints "an array"
a = 1;
a.test(); // prints "a number"
a = "";
a.test(); // prints "an object"
String.prototype.test = function(){ console.log('a string')};
a.test(); // prints "a string"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment