"this" keyword in a javascript module
I have defined the following module globally:
var module = (function () {
console.log(this);
this.fn = function () {
console.log(this);
}
return this;
})();
http://www.quirksmode.org/js/this.html :
In JavaScript |this| always refers to the "owner" of the function we're
executing, or rather, to the object that a function is a method of.
The first call to console.log logs Window as the value of this, and that I
understand. But, so does also the second call to console.log.
Since this refers to the owner of the function, why does module.fn log
Window and not module?
When I call fn I still have to write module.fn, I can't write Window.fn.
Since this refers to Window I find this confusing.
EDIT: I forgot to return this in my example.
No comments:
Post a Comment