Size: 998
Comment:
|
Size: 1991
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 5: | Line 5: |
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. | 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. |
Line 14: | Line 14: |
* camelCase variables and function names * CapitalCamelCase for classes * [[http://en.wikipedia.org/wiki/Indent_style#Compact_control_readability_style|Compact Control]] indention * Semicolons are good |
{{{ 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 |
Line 19: | Line 39: |
* `===` for comparisons | * Triple equals (`===`) for comparisons * Single quotes for strings * Same-line brackets |
Line 27: | Line 49: |
* @param foo - task selector | * @param options - possible keys include: * * myMessage (optional) - an example message |
Line 29: | Line 52: |
function exampleTask(foo) { var myVar = 'My message'; |
ROBOTJS.MyAwesomeClas = function (options) { options = options || {}; var myMessage = options.myMessage || 'My message'; |
Line 32: | Line 56: |
if (foo === 'Do this') { anotherExampleTask(myVar); |
if (myMessage === 'Do this') { alert('Not a pubic function'); |
Line 36: | Line 60: |
someOtherTask(myVar); | this.myPublicFunction(myVar); |
Line 38: | Line 62: |
} | }; /** * Alert a string in this public function. * * @param str - a string to alert */ ROBOTJS.MyAwesomeClas.prototype.myPublicFunction = function(str) { alert(str); }; |
Line 40: | 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.