Created
July 18, 2014 13:38
-
-
Save CharlotteGore/0c406c55b36eca8941fe to your computer and use it in GitHub Desktop.
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
function Tiger (){ | |
this.name = ""; | |
this.isKing = ""; | |
return this; | |
} | |
Tiger.prototype = { | |
setName : function (name){ | |
this.name = name; | |
}, | |
getName : function (){ | |
return this.name + (this.isKing ? ", who is the king": ", who is a nobody"); | |
} | |
} | |
// what if we do it this way? | |
Tiger.coronate = function (){ | |
this.isKing = true; | |
} | |
var tigerKing = new Tiger(); | |
tigerKing.setName('Tiger King'); | |
tigerKing.coronate(); // Aghg!! Error! Undefined!!! | |
tigerKing.getName(); // "Tiger King, who is a nobody" | |
// .coronate is actually a static method on the Tiger function. Boo. It doesn't exist for instances of Tiger like tigerKing. | |
// using sneaky tricks we can 'call' coronate and have it work on our tigerKing object... but this is advanced stuff. | |
Tiger.coronate.call(tigerKing); | |
tigerKing.getName(); // "Tiger King, who is the king" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment