Manual talk:Coding conventions/JavaScript
From MediaWiki.org
Contents
Thread title | Replies | Last modified |
---|---|---|
Closures | 0 | 21:08, 20 December 2011 |
I've changed the recommended closure format based on what I've recently seen used in core and as well as what I'm finding myself use.
Just shortly explaining in case anyone is interested.
- Closure: To prevent leakage from and to other scopes
- Arguments: Primarily for performance reasons so that
jQuery
andmw
are available in the closest scope (or at least a scope closer than the global scope) instead of accessing the data from the global scope every time.[1] It's also for shortness so thatjQuery
is abbreviated to$
. And to enforce that undefined is really undefined. - Callee: Using the global literals instead of properties of the
window
object. Reason being that if it would be undefined, it's better to catch the error at the ReferenceError in the callee then somewhere down the line where a property or function call is performed on "undefined". It also saves an additional property lookup after doing the scope chain lookup for "window".
References:
- ↑ "Scope Chains" in Nicholas C. Zakas - Speed Up Your JavaScript, Google Tech Talk (June 4, 2009 )