• Diff for "JavaScriptStyleGuide"
Differences between revisions 6 and 8 (spanning 2 versions)
Revision 6 as of 2013-04-29 17:09:23
Size: 1728
Comment:
Revision 8 as of 2013-04-29 17:44:27
Size: 1991
Comment:
Deletions are marked like this. Additions are marked like this.
Line 62: Line 62:
} };
Line 73: Line 73:

== Linting ==
Linting of `JavaScript` files should be done using [[http://www.jshint.com/|JSHint]]. The standard configuration file can be found [[https://github.com/RobotWebTools/starter-template/blob/stable/utils/.jshintrc|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.

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