I can definitely see the usefulness of closing over "this". When I
first started learning about closures in JavaScript, I was miffed by
the fact that "this" was skipped over. "What's the point?" I thought.
As I've come to understand the language better, my view has changed a
bit. Closing over "this" makes the behavior of the call and apply
methods ambiguous, plus it makes the effect of many syntactical
statements extremely confusing to understand. Consider the following
example:
function MyClass() {
this.callback1 = function() {
debug.write(this.whoAmI);
}
this.whoAmI = "MyClass instance";
}
MyClass.prototype.callback2 = function() {
debug.write(this.whoAmI);
}
function Distributor() {
this.callbacks = [];
this.callbacks.whoAmI = "callbacks array";
this.addCallback = function(callback) {
this.callbacks.push(callback);
}
this.execute = function() {
for( var i = 0; i < this.callbacks.length; i++ ) {
this.callbacks[i]();
}
}
this.whoAmI = "Distributor instance";
}
_root.whoAmI = "_root";
var inst = new MyClass();
var dist = new Distributor();
dist.addCallback(inst.callback1);
dist.addCallback(inst.callback2);
dist.execute();
In regular JavaScript, you'll see this in the debug window:
callbacks array
callbacks array
If you close over "this", I would guess perhaps the above, or maybe
the following:
MyClass instance
callbacks array
Or possibly even:
MyClass instance
_root
But almost certainly not:
MyClass instance
MyClass instance
The asymmetry is positively brain-busting. My coworkers complain to
me enough about not understanding what "this" refers to in various
pieces of my code. Closing over "this" would increase that confusion
10x. (And it would almost certainly break some portion of our
existing code base.)
On Dec 1, 2005, at 11:44 AM, P T Withington wrote:
Well, I think that having closing over a method close over `this`
is actually right and I don't think it would actually break
anything. (I believe the ActionScript extension applies _only_ to
methods; and methods _do_ have a free reference to this. Really
all they have done is made it so that if you close over a method
`foo`, that you can invoke it as `foo()` and not have this be `null`.)
On 1 Dec 2005, at 11:51, Neil Mix wrote:
Wow. I'm stunned. Is it possible for the compiler do the right
tricks in order to maintain x-platform compatibility within LZX?
I know this is still a long way off, I'm just curious.
On Dec 1, 2005, at 9:59 AM, P T Withington wrote:
On 1 Dec 2005, at 10:43, Neil Mix wrote:
off-topic: P.T., if I understand you correctly, in swf9 the
"this" that is present when a function is defined is what gets
used when the function is executed? I've never seen a browser
JavaScript implementation do that -- it seems that would break
the language in some pretty fundamental ways.
It _is_ a deviation from Javascript. It is a Macromedia
extension (perhaps taken from the Javascript 2 proposal?),
specifically intended to eliminate the need for delegates. It
applies to closed over methods only. Cf. http://
labs.macromedia.com/wiki/index.php/ActionScript_3:overview (and
search for 'closure').
_______________________________________________
Laszlo-dev mailing list
[email protected]
http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
_______________________________________________
Laszlo-dev mailing list
[email protected]
http://www.openlaszlo.org/mailman/listinfo/laszlo-dev