By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

this-keyword in anonymous functions

P: n/a
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

Apr 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Apr 11, 1:35 am, Sebastian Morawietz
<sebastian.morawi...@googlemail.comwrote:
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.
In this case, object properties of an anonymous object - the obvious
solution is don't use an anonymous object where it would be much
easier to use one with a name. :-)

Dunno about "best practice", but a solution (not necessarily best, or
perhaps even good) is:

function test() {
({

_call: function(that, fn) {
fn.call(that);
},

test: function() {

/* Position A */
console.log(this);

// Just for the example
var self = this;

this._call(this, function() {

/* Position B */
console.log(this == self); // true
});
}
}).test();
}
--
Rob
Apr 11 '08 #2

P: n/a
On Apr 11, 4:35 pm, Sebastian Morawietz
<sebastian.morawi...@googlemail.comwrote:
On 11 Apr., 05:22, RobG <rg...@iinet.net.auwrote:
On Apr 11, 1:35 am, Sebastian Morawietz
Dunno about "best practice", but a solution (not necessarily best, or
perhaps even good) is:
function test() {
({
_call: function(that, fn) {
fn.call(that);
},
test: function() {
/* Position A */
console.log(this);
// Just for the example
var self = this;
this._call(this, function() {
/* Position B */
console.log(this == self); // true
});
}
}).test();
}

Thanks for the answer. A closer look to Prototype's sources revealed
to me, that this is basically, how they do it with their iterators
too.
Do they? Dang, that's almost certain to mean it's not in the "best
practice" list of anyone here. :-)

One way to get anonymous iterators to call themselves is with
arguments.callee, but that doesn't get the "parent" object, which is
what you seem to be after.
--
Rob
Apr 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.