I have read a couple of articles on the web explaining this
in Javascript. While the articles
have helped a lot, the behavior shown below is still unclear to me.
Here it says:
In the global execution context (outside of any
function
), this refers to the global object, whether in strict mode or not.
If so, can someone please explain the behavior (noted in the comments) when the following code is run with node.
console.log(this); // Returns an empty object: {}.
// Why does this line not return the global object.
var somefunc = function(name) {
console.log(this);
}
somefunc(); // Returns the the global object. I think I understand this. The function is
// invoked in a global context.
somefunc.call(this); // Again returns the empty object. Why?
Thanks for your help.
EDIT (as requested by below by the moderators) *How is this question and the chosen answer different from the one linked above*
I think both the question and certainly the answer here is clearer than the one that is considered a duplicate. The answer here clarifies what node is doing by giving example code, which is more helpful.