News for hackers. Stuff that matters.
Re:An Introduction to JavaScript's "this" by aboodman on Saturday September 26, 2009 @10:43PM (#846439)
BTW, imo, the behavior of |this| is a really unfortunate design flaw of JS. The amount of time that must have been spent learning and teaching this edge case over and over to every person new to the language is ... well, it's big.
Re:An Introduction to JavaScript's "this" by vicaya on Saturday September 26, 2009 @11:37PM (#846493)
The behavior of 'this' is from DOM bindings and not from the language per se. Javascript can set 'this' of any calling context to any object with 'apply' or 'call'. The convention for on* handlers is that 'this' is set to the DOM element when the handlers are called. I only learned to appreciate this, when I wrote my own little JS DOM framework a la JQuery or Dojo.
Re:An Introduction to JavaScript's "this" by litewulf on Sunday September 27, 2009 @02:45AM (#846604)
I believe actually that 'this' points to the global object.

So if there is no obvious thing for this to point at, it'll point at the global object. Its just implementation detail that in browsers it points to the window object.

Re:An Introduction to JavaScript's "this" by Bluem00 on Saturday September 26, 2009 @11:37PM (#846492)
Do you know why it was designed this way? Was 'new' added to javascript after the behavior of 'this' was already well defined and the creators did not feel comfortable changing it?
Re:An Introduction to JavaScript's "this" by jmtulloss on Sunday September 27, 2009 @06:36AM (#846745)
actually, no. As I understand it, the language was supposed to be some sort of weird conglomeration between traditional object oriented languages and dynamic, lisp-ish languages. The problem was that the creators had about 30 seconds to get this done and they could make things look more "normal" by not explicitly supporting every feature they envisioned.

"new", I believe, is actually an ancient relic of the prototypal class system. That's another topic entirely, but it's incredibly cool. Check out Douglas Crockford's articles on inheritance if you want closure on the issue. He has written way more than I have on the topic.

Re:An Introduction to JavaScript's "this" by praptak on Sunday September 27, 2009 @10:41AM (#846883)
"new", I believe, is actually an ancient relic of the prototypal class system.

I believe it is quite the opposite. It was a failed attempt to make the prototype-based object system look like class-based one. I vaguely remember reading this opinion in Crockford's "Javascript:The Good Parts" book.

Re:An Introduction to JavaScript's "this" by jmtulloss on Sunday September 27, 2009 @03:34PM (#847165)
Agreed. That's actually what I was trying to say. It's an ancient relic of JavaScript's implementation of the class system.