Skip to content

Instantly share code, notes, and snippets.

@phlik
Last active January 2, 2016 04:29
Show Gist options
  • Save phlik/8250734 to your computer and use it in GitHub Desktop.
Save phlik/8250734 to your computer and use it in GitHub Desktop.
Simple script to help others understand how "this" works.
window.name = 'windo';
function Taco(name) {
this.name = name;
var nameCool = name + ' cool';
function PrintNameThis() {
console.log(this.name);
}
this.showTheWindowScope = function() {
PrintNameThis();
};
var variblePrintFunc = function() {
console.log(this.name);
};
this.showVaribleFuncScope = function(){
variblePrintFunc();
};
this.showThisPrintFunc = function(){
console.log(this.name);
};
this.localScopePrint= function(){
console.log(nameCool);
};
return this;
}
Taco.prototype.protoLocPrintCall = function(){
console.log(nameCool);
};
Taco.prototype.protoThisPrintCall = function(){
console.log(this.name);
};
var tc = new Taco("food for thought");
tc.showTheWindowScope();
tc.showVaribleFuncScope();
tc.showThisPrintFunc();
tc.protoThisPrintCall ();
tc.localScopePrint();
tc.protoLocPrintCall();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment