this-keyword in anonymous functions

  • Thread starter Sebastian Morawietz
  • Start date
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
 
R

RobG

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();
}
 
S

Sebastian Morawietz

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. I missed the part, that I can pass a context as second parameter,
and get the iterator bound to it - this pretty much solves my
problem :).

Greets
Seb
 
R

RobG

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top