Last active
August 29, 2015 13:57
-
-
Save detournemint/9357590 to your computer and use it in GitHub Desktop.
Explanation of This in Javascript
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
This is set when function is called | |
This also depends on HOW it is called. Not how it is defined. | |
Examples = | |
constructor => this = new object ({}) | |
var test = new Test(); | |
method => this = receiver (test) | |
test.something() | |
function => this = window | |
var myFunc = clock.run; | |
myFunc() | |
call/apply => this = first arg (obj) | |
myFunc.call(obj) | |
myFunc.apply(obj) | |
To solve the problem of wrong "this" | |
Use: | |
var that = this; | |
Funct(function () {that.funct2();}, x) | |
Or using Bind | |
var func = this.func2.bind(this); | |
setFunct(func, x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment