Last active
January 2, 2016 04:29
-
-
Save phlik/8250734 to your computer and use it in GitHub Desktop.
Simple script to help others understand how "this" works.
This file contains 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
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