Skip to content

Instantly share code, notes, and snippets.

@detournemint
Last active August 29, 2015 13:57
Show Gist options
  • Save detournemint/9357590 to your computer and use it in GitHub Desktop.
Save detournemint/9357590 to your computer and use it in GitHub Desktop.
Explanation of This in Javascript
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