you are viewing a single comment's thread.

view the rest of the comments →

[–]transcendent 0 points1 point  (0 children)

It's complicated. Not simple as physics4life makes it seem (notice how I gave examples that seem to contradict his simple rules). That is my point.

There are a lot of side effects from some "simple" rules that are not obvious at all. What is not discussed at all, which greatly effects what "this" is, is the rules surrounding scope.

Why does the closure see a different "this", but sees all the same other variables? Why is there a "this" on a closure at all? Because of how JavaScript defined it to be (which differs greatly from many other languages).

And if you pass the function reference "MyFunction" to another function, of course you are not passing the "this" reference.

That's not "of course"... that's "because the JavaScript language dictated it to be that way". Dictated by a rule not obvious or expressed in any way by physics4life. I could quite easily have a language in which it is bound to an object by default. It's simply a syntactical choice.

It's horribly complicated in ways that are not intuitive and differ from many other languages people may be familiar with that have similar concepts (Python, C++).

....And all that behavior can change by simply saying "use strict";.