LWN.net Logo

Object oriented Lua

Object oriented Lua

Posted Nov 19, 2011 9:46 UTC (Sat) by alankila (subscriber, #47141)
In reply to: Object oriented Lua by man_ls
Parent article: Awesome: A window manager that gets out of the way

The "this" reference does not disappear. It's just always set on every method call, and in this case it will be the contained DOM node, as you'd expect (it's the DOM node's method being called). You simply capture that other object into a variable -- a strategy you might not like -- but it's not unreasonable.

Incidentally, Java would allow people to write Node.this to represent the Node object, assuming the function objects were written as anonymous inner classes.


(Log in to post comments)

Object oriented JavaScript

Posted Nov 19, 2011 13:02 UTC (Sat) by man_ls (subscriber, #15091) [Link]

It is especially annoying in callbacks: I would expect that this would be part of the function context as any other closure, but no: this points to the DOM node where the callback is running.

It can be worked around, of course: using a real closure, or calling the callback using weird call() techniques. But it makes for one of those "what were they thinking" moments. Same for prototypes, metatables' bastard son.

Frankly, JavaScript is surprisingly sane given that it was invented in 10 days.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds