Skip to content

Instantly share code, notes, and snippets.

@PavanKu
Created December 26, 2017 05:42
Show Gist options
  • Save PavanKu/b3d131893f7f602b3c454c6a69ba9c9a to your computer and use it in GitHub Desktop.
Save PavanKu/b3d131893f7f602b3c454c6a69ba9c9a to your computer and use it in GitHub Desktop.
this inside object method
function foo () {
'use strict';
console.log("Simple function call")
console.log(this === window);
}
let user = {
count: 10,
foo: foo,
foo1: function() {
console.log(this === window);
}
}
user.foo() // Prints false because now “this” refers to user object instead of global object.
let fun1 = user.foo1;
fun1() // Prints true as this method is invoked as a simple function.
user.foo1() // Prints false on console as foo1 is invoked as a object’s method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment