too many that=this ... better solution for calling methods in lambdas?

M

Matìj Cepl

Hi, I have many calls of methods inside of lambda functions in a object
method:

obj.prototype.methodA = function() {
var that = this;
... doing something ...
list.forEach(function(x) { // or it might be a callback in XHR
that.methodB(x);
}
.... doing something else ...
}

I am slightly bothered about all those that = this lines. There are too
many of them (basically one in every non-trivial function I have). Do I
something wrong? Is there a better way how to achieve the same result
with some less cludgy means?

In case it is FAQ (as it should be, but I haven't found it anywhere), I
am sorry and please point me to the URL of the FAQ explaining this.

Thanks a lot,

Matìj
--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

Besides, the determined Real Programmer can write Fortran
programs in any language.
-- Ed Post, Real Programmers Don't Use Pascal
 
M

Matt Kruse

Hi, I have many calls of methods inside of lambda functions in a object
method:
[...]
I am slightly bothered about all those that = this lines.

I would be too. I always prefer 'self'. :)
There are too
many of them (basically one in every non-trivial function I have). Do I
something wrong? Is there a better way how to achieve the same result
with some less cludgy means?

I don't see this as being cludgy. It's just how things are done
because of the nature of the language. It's straight-forward and easy
to understand. Embrace it.

Of course, you could do something like this instead:

obj.prototype.methodA = function() {
(function(obj) {
list.forEach(function(x) { // or it might be a callback in XHR
obj.methodB(x);
}
})(this);
}

.... but I suspect that would be seen as even more cludgy. :)

Matt Kruse
 
M

Michael Haufe (\TNO\)

Hi, I have many calls of methods inside of lambda functions in a object
method:

obj.prototype.methodA = function() {
        var that = this;
        ... doing something ...
        list.forEach(function(x) { // or it might be a callback in XHR
                that.methodB(x);
        }
        .... doing something else ...

}

I am slightly bothered about all those that = this lines. There are too
many of them (basically one in every non-trivial function I have). Do I
something wrong? Is there a better way how to achieve the same result
with some less cludgy means?


forEach has a second argument you can use:

obj.prototype.methodA = function() {
list.forEach(function(x) {
this.methodB(x);
},this);
}

There is also the apply method you could use for similar effect:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply
 
D

David Mark

Hi, I have many calls of methods inside of lambda functions in a object
method:

obj.prototype.methodA = function() {
        var that = this;
        ... doing something ...
        list.forEach(function(x) { // or it might be a callback in XHR
                that.methodB(x);
        }
        .... doing something else ...

}


https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/ForEach

list.forEach(function(x) {
this.methodB(x);
}, this);
 
M

Matìj Cepl

Dne 10.12.2009 17:25, Michael Haufe ("TNO") napsal(a):
forEach has a second argument you can use:

obj.prototype.methodA = function() {
list.forEach(function(x) {
this.methodB(x);
},this);
}

AWESOME! Thanks.

And yes that's exactly what I need. And I would swear I read that
forEach page 50 times. Oh well :(

Thanks again,

Matìj
--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

I know of no country in which there is so little independence of
mind and real freedom of discussion as in America.
-- Alexis de Tocqueville
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top