• Diff for "JavaScriptStyleGuide"
Differences between revisions 7 and 9 (spanning 2 versions)
Revision 7 as of 2013-04-29 17:42:50
Size: 1990
Comment:
Revision 9 as of 2013-04-29 17:46:22
Size: 2258
Comment:
Deletions are marked like this. Additions are marked like this.
Line 62: Line 62:
} };
Line 76: Line 76:

== Building ==
Building of tools and libraries should be done using [[http://gruntjs.com/|Grunt]]. An example setup, installation, and usage guide can be found [[https://github.com/RobotWebTools/starter-template/blob/stable/utils|on the Robot Web Tools GitHub]].

ROS JavaScript Style Guide

This page defines a style guide to be followed in writing JavaScript code for ROS. This guide applies to all ROS code, both core and non-core.

For Python, see the Python Style Guide and for C++, see the C++ Style Guide

Coding Style

A quick summary is:

  • 2 space indention
    • function foo() {
        var indentation = 2;
      }
  • Camel case variables and function names
    • var myNewVariable = 'robots';
  • Capital camel case for classes
    • MyNewClass = function() { ... };
  • Single-word caps for namespaces (typically ending in JS)

    • ROBOTJS.MyNewClass = function() { ... };
  • Underscored caps for constants
    • ROBOTJS.MY_NEW_CONST = 3.14;
  • Options object for all class constructors
  • Public functions should use prototype

  • Semicolons are required
  • JSDoc for documentation

  • Triple equals (===) for comparisons

  • Single quotes for strings
  • Same-line brackets

An example can be worth a thousand words:

/**
 * Performs an example task.
 *
 * @param options - possible keys include:
 *   * myMessage (optional) - an example message
 */
ROBOTJS.MyAwesomeClas = function (options) {
  options = options || {};
  var myMessage = options.myMessage || 'My message';

  if (myMessage === 'Do this') {
    alert('Not a pubic function');
  }
  else {
    this.myPublicFunction(myVar);
  }
};

/**
 * Alert a string in this public function.
 *
 * @param str - a string to alert
 */
ROBOTJS.MyAwesomeClas.prototype.myPublicFunction = function(str) {
  alert(str);
};

Linting

Linting of JavaScript files should be done using JSHint. The standard configuration file can be found on the Robot Web Tools GitHub.

Building

Building of tools and libraries should be done using Grunt. An example setup, installation, and usage guide can be found on the Robot Web Tools GitHub.

Wiki: JavaScriptStyleGuide (last edited 2014-11-07 15:16:53 by Russell Toris)