Created
December 26, 2017 05:42
-
-
Save PavanKu/b3d131893f7f602b3c454c6a69ba9c9a to your computer and use it in GitHub Desktop.
this inside object method
This file contains hidden or 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
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