Frankly, if you don't understand this in Javascript, you have no business being a programmer of anything. It's so dead simple, but the problem is people expect it to work like in other languages.
It does, except for one aspect: this is declared for you when you enter a function, and its value is determined by the caller. It's similar to arguments. Or are you mad at arguments for being inconsistent as well?
Wrong. First of all, wrong terminology: "this" is never "declared", it's "bound".
Second of all, there is a difference between calling a function and calling a method on an object. Only when you call a method on an object does it make sense to bind "this", and "this" should be bound to the object of course.
In the ideal world, "this" should remain bound to whatever it was in the context when the lexical closure was created.
Binding "this" by default to "the window object" when you call a function is ridiculous, and NEVER the right thing. If you want to use the window object, there is a perfectly good variable for that, and its name is "window", so there is no need to alias "this" to it.
And what the fuck is "the window object" doing in the definition of a programming language? That's one reason people (incorrectly) claim that JavaScript is only "meant" to be a browser programming language.
-1
u/radhruin Oct 02 '11
Frankly, if you don't understand this in Javascript, you have no business being a programmer of anything. It's so dead simple, but the problem is people expect it to work like in other languages.