On Thu, Dec 30, 2010 at 2:48 PM, Tom Wilson <[email protected]> wrote:
> I just finished reading "High Performance JavaScript" and now I'm paying > more attention to scope. So my question is: within an object method, is > there any benefit to declaring a local variable referencing 'this'? Is that > just redundant? What about a local variable referencing a property of 'this' > that is itself an object? Like var myLocal = this.someProperty so that you > can refer to myLocal.x, myLocal.y, etc. > > The short answer is "no". This, in terms of speed, `this` is equal to a regular variable. Making an alias is only going to cause (more?) confusion. Caching a property lookup is usually not really worth it*. Best consideration for doing it anyways is clean code. But don't do it for speed. * Not the case on so called "live (dom) queries", like the result of `document.getElementsByTagName()`, but that's more of an exception - peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
