# CONTRIBUTING
- Complete the Contributor License Agreement.
- Read the coding standards below
- Make a pull request
Feel free to contribute bug fixes or documentation fixes directly into a pull request. If you are making major changes to the engine or adding significant new features. It would be best to create an github issue before starting work to begin a conversation about how best to implement it.
These coding standards are based on the Google Javascript Coding Standards. If something is not defined here, use this guide as a backup.
For example, use "Initialize" instead of "Initialise", and "color" instead of "colour".
For example:
// Notice there is no new line before the opening brace
function inc() {
x++;
}
Also use the following style for 'if' statements:
if (test) {
// do something
} else {
// do something else
}
Ensure that your editor of choice is set up to insert '4 spaces' for every press of the Tab key. Different browsers have different tab lengths and a mixture of tabs and spaces for indentation can create ugly results.
Set your text editor to remove trailing spaces on save
for (var i = 0, len = list.length; i < len; i++) {
// ...
}
var fn = function () {
};
Semicolons are not needed to delimit the ends of functions. Follow the convention below:
function class() {
} // Note the lack of semicolon here
Semicolons are needed if you're function is declared as a variable
var fn = function () {
}; // Note the semicolon here
var NameSpace = function () {
// Private variables
// Public interface
return {
arrive : function () {
console.write("Hello");
},
depart : function () {
console.write("Goodbye");
}
}
}
Variable declarations should all be placed first or close to the top of functions. This is because variables have a function-level scope.
Variables should be declared one per line.
function fn() {
var a = 0;
var b = 1;
var c = 2;
}
function loop() {
var i = 100;
for(var i = 0; i < 100; ++i) { // Don't do this. The same i var as declared at the top
}
}
// Namespace should have short lowercase names
var namespace = {};
// Classes (or rather Constructors) should be CamelCase
var MyClass = function () {};
// Variables should be mixedCase
var mixedCase = 1;
// Function are usually variables so should be mixedCase
// ( unless they are class constructors )
var myFunction = function () {};
// Constants should be ALL_CAPITALS separated by underscores.
// Note, javascript doesn't support constants in a cross-browser fashion,
// so this is just convention.
var THIS_IS_CONSTANT = "well, kind of";
// Private variables should start with a leading underscore.
// Note, you should attempt to make private variables actually private using
// a closure.
var _private = "private";
var _privateFn = function () {};
Treat acronyms like a normal word. e.g.
var json = ""; // not var JSON = "";
var id = 1; // not var ID = "";
function getId() {}; // not getID()
function loadJson() {}; // not loadJSON();
new HttpObject(); // not new HTTPObject();
function asyncFunction( success, error ) {
// do something
}
function asyncFunction( success ) {
// do something
}
function asyncFunction( callback ) {
// do something
}
It is often useful to be able to cache the 'this' object to get around the scoping behavior of Javascript. If you need to do this, cache it in a variable called 'self'.
var self = this;
Hide variables that should not be accessible using a closure
var Class = function () {
var _a = "private";
this.getA() { return a; }
}
The hasOwnProperty() function should be used when iterating over an object's members. This is to avoid accidentally picking up unintended members that may have been added to the object's prototype. For example:
for (var key in values) {
if (values.hasOwnProperty(key)) {
doStuff(values[key]);
}
}
Filenames should be all lower case with words separated by underscores. The usual format should be {{{namespace_class.js}}}
e.g.
math_matrix.js
scene_graphnode.js
Create namespaces using a function so that you can expose a public interface and have private functions.
var namespace = function () {
// Private
function privateFn() {
};
// Public interface
return {
publicVar: "public",
publicFn: function () {
privateFn();
}
};
}();
Class constructors are declared in the same way as public functions. Use pc.inherits
to derive from a base class and use pc.extends
to extend the prototype with new methods.
Only declare one Class per file.
var namespace = function () {
var Class = function () {
var _private = "private";
this.accessor = function () {
return _private;
};
}
};
Class = pc.inherits(Class, Base);
pc.extends(Class.prototype, {
derivedFn: function () {
}
});
return {
Class: Class
};
}();
Private functions and variables should be declared inside the namespace.
Public functions and variables should be returned from the function that creates the namespace.
var namespace = function () {
// private
function privateFn () {}
// public interface
var Class = function () {
};
Class = pc.inherits(Class, Base);
// public prototype functions
pc.extends(Class.prototype, {
derivedFn: function () {
}
});
return {
publicVar: "public",
publicFn: function () { return "public function"; },
Class: Class
};
}();
Use library function pc.extend to add additional Classes, methods and variables on to an existing namespace
pc.extend(namespace, function() {
var Class = function () {
};
Class = pc.inherits(Class, Base);
Class.prototype.derivedFn = function () {
};
return {
Class: Class
};
} ());