S
Sebastian Morawietz
Hi,
given this code:
function test() {
({
call: function(fn) {
fn();
},
test: function() {
/* Position A */
Logger.log(this);
this.call(function() {
/* Position B */
Logger.log(this);
});
}
}).test();
}
Obviously, "this" at Position A points to a different object, than at
Position B. At Position B it's the DOMWindow-Object ( at least in
Safari ) or a the global Context in a standalone SpiderMonkey-Engine.
This makes writing code, that deals alot with Prototype-Library-like
iterators pretty error-prone. Up to now I handle this by defining a
local variable "self", and let the closure-mechanics deal with the
rest, but I hate the way that looks ("this" here, "self" there ), and
are really wondering, if there is a better way/best practice to access
object-properties in an anonymous function.
Greets
Sebastian
given this code:
function test() {
({
call: function(fn) {
fn();
},
test: function() {
/* Position A */
Logger.log(this);
this.call(function() {
/* Position B */
Logger.log(this);
});
}
}).test();
}
Obviously, "this" at Position A points to a different object, than at
Position B. At Position B it's the DOMWindow-Object ( at least in
Safari ) or a the global Context in a standalone SpiderMonkey-Engine.
This makes writing code, that deals alot with Prototype-Library-like
iterators pretty error-prone. Up to now I handle this by defining a
local variable "self", and let the closure-mechanics deal with the
rest, but I hate the way that looks ("this" here, "self" there ), and
are really wondering, if there is a better way/best practice to access
object-properties in an anonymous function.
Greets
Sebastian