Skip to content

Instantly share code, notes, and snippets.

@CharlotteGore
Created July 18, 2014 13:38
Show Gist options
  • Save CharlotteGore/0c406c55b36eca8941fe to your computer and use it in GitHub Desktop.
Save CharlotteGore/0c406c55b36eca8941fe to your computer and use it in GitHub Desktop.
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