Manual talk:Coding conventions/JavaScript

From MediaWiki.org
Jump to: navigation, search
Start a new discussion

Contents

Thread titleRepliesLast modified
Closures021: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 and mw 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 that jQuery 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:

  1. "Scope Chains" in Nicholas C. Zakas - Speed Up Your JavaScript, Google Tech Talk (June 4, 2009 )
Krinkle21:02, 20 December 2011
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox