3 ms·
I don't get the closure question. How are the two logs different?
by maruhan2 11y ago
I don't get the closure question. How are the two logs different?
- swang 11y ago`this` in javascript is tied to whatever object is calling it. For `User.getCount`, the object is User, but when you declare `func = User.getCount`, and then call `func()`, you are now calling from whatever object `func` belongs to (in this case, the "global" object)
- gefh 11y agoThe first call is to User.getCount() - 'this' refers to User, so the function returns 1. The second call is to a bare function - or more exactly, a function on the global scope - so 'this' refers to the global object, which is window in a browser. So it returns undefined, as there is no window.count.
- deleted 11y ago[deleted]