Last active
December 18, 2015 11:38
-
-
Save fabriceleal/5776544 to your computer and use it in GitHub Desktop.
Polymorphism in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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