Gaia/ExistingStylesPatterns

From MozillaWiki
< Gaia
Revision as of 03:39, 20 November 2013 by Snowmantw (talk | contribs) (init)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Context

Since every reviewer has his/her concerns on every patch, we really need a page to collect existing accepted styles and patterns in the Gaia code base. It's really upset to be rejected just because of the differences between opaque personal opinions of different reviewers.

Please help to contribute to this page to make this more clear and open.

Common Principles

Naming the Anonymous Functions

Anonymous functions should be named, even some reference has already be bound on it:

 var tsEnd = function _tsEnd(evt) {
   // handle the 'transitionend' event here.
 }

Methods belong to components should be named, too. No matter whether it's in JSON or other format:

 init: function ls_init() {
   // initialize the lockscreen
 }
 AppWindow.prototype.focus = function aw_focus() {
   // do focus on the app window
 }

You can notice that these two functions' name has been prefixed with the abbreviation of the component. This is a trick for the lack of namespace in JavaScript.

This would be outdated if the Bug 883377 have been done. This feature make function name can be bound on the reference assign to it:

 var hello = function(){console.log('Hello')};
 // console would know that "assignment" says 'Hello'

This is necessary for ES6's fat arrow function:

 prop = () => {};    // "prop"

Because you can't assign a name to an arrow function as before:

 prop = prop() => {};    // Illegal; syntax error.