and don't forget bind: myFunc = foo.bind(bar)
baz.foo = myFunc baz.foo() // this === bar, not baz Am Freitag, 14. Dezember 2012 00:21:19 UTC+1 schrieb Michael Jackson: > > The best explanation I've ever heard for "this" in JavaScript: > > > The word "this" refers to whatever was on the left of the period when > the function was called. If nothing was, it refers to the global scope. > > Obviously this is a bit simplified because it doesn't take into account > Function#call/apply, but it is sufficient to explain the concept most of > the time. So, yes, "this" can be very useful if you know what is going to > be to the left of the period. It's also especially useful in functions that > are members of another function's prototype. > > -- > Michael Jackson > @mjackson > > > > On Tue, Dec 11, 2012 at 1:46 PM, spqr <[email protected] <javascript:>>wrote: > >> I'm writing a fairly unusual type of node program that manages and runs >> JavaScript objects that the master program loads at start-up. >> I don't want to go into more detail than that, but I do want to make one >> comment. It seems to me that "this" as it is defined in >> JavaScript, is close to useless. Because I load my objects dynamically >> and call their functions via eval(), when they need to run, >> "this" never has a useful value. Sure, I worked around it by getting a >> reference to the object I really want and passing it in to the >> function as an argument (annoying and ugly, but workable). I'm just >> curious if anyone really thinks 'this" is useful. I've seen the >> construct where you set a var in the object itself that is a reference to >> the object... honestly, that looks pretty "off" itself, but also >> doesn't work when eval() is involved. >> >> Is "this" in JavaScript really useful for anything? >> >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> You received this message because you are subscribed to the Google >> Groups "nodejs" group. >> To post to this group, send email to [email protected]<javascript:> >> To unsubscribe from this group, send email to >> [email protected] <javascript:> >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> > > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
