Skip to content

Instantly share code, notes, and snippets.

@dagolinuxoid
Last active January 27, 2017 07:06
Show Gist options
  • Save dagolinuxoid/49f22f6fab50114b432b75ea182f0f2a to your computer and use it in GitHub Desktop.
Save dagolinuxoid/49f22f6fab50114b432b75ea182f0f2a to your computer and use it in GitHub Desktop.
#this #call #bind #apply
var obj = { name: 'smith' };
function outer() {
(function inner() {
console.log(this.name);
}).call(this);
}
function outer() { // same result as above
(function inner() {
console.log(this.name);
}.bind(this))();
}
outer.apply(obj);
function another() {
function inna() {
console.log(this.lag); // we don't know yet which value `this` will represent unless
//the act of invocation inna function happens.
}
inna.call(this); // what value `this` gets in parenthesis has determined
// by a manner in which another function was called.
// only after we have figured out the value of `this` in parenthesis, we
// use it in executing code of our inna function.
}
another.call(obj); // since we're using .call method and putting obj as argument,
// 'this' in parenthesis will apply it as its value and never changes in duration
// of a function run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment