wiki:WebInspectorCodingStyleGuide

Version 3 (modified by Joseph Pecoraro, 10 years ago) (diff)

--

These are JavaScript coding styles used in the Source/WebInspectorUI/UserInterface folder.

  • No trailing whitespace
  • Indent with 4 spaces
  • The opening bracket "{" after a named function goes on the next line. Anywhere else, the opening bracket "{" stays on the same line.
  • Style for JavaScript Object Literals is: {key1: value1, key2: value2}.
  • Inline anonymous functions, especially if they don't need a .bind(). Example:
        this.requestRootDOMNode(function(rootNode) {
            ...
        });
    
  • Avoid using the "on" prefix where possible. The "_onFoo" methods can just be "_foo".
  • New class names should use the name of the base class as a sufix.
  • Add new lines before and after different tasks performed in the same function.
  • Consider using Map structure instead of Object.
  • Consider using rgb() over hex colors in CSS
  • Firewall the protocol inside the Manager classes. JSON objects received from the protocol are called "payload" in the code. The payload is usually deconstructed at the Managers level and passes down as smart objects inheriting from WebInspector.Object.

The new JS object types should have the following format:

WebInspector.NewObjectType = function()
{
    WebInspector.Object.call(this);
    this._propertyName = ...;
}

WebInspector.NewObjectType.Event = {
    PropertyWasChanged: "new-object-type-property-was-changed"
};

WebInspector.NewObjectType.prototype = {
    constructor: WebInspector.NewObjectType,
    __proto__: WebInspector.Object.prototype,

    // Public

    get propertyName()
    {
        return this._propertyName;
    },

    set propertyName(value)
    {
        this._propertyName = value;
        this.dispatchEventToListeners(WebInspector.NewObjectType.Event.PropertyWasChanged);
    },

    publicMethod: function()
    {
        /* public methods called outside the class */
    }

    // Private

    _privateMethod: function()
    {
        /* private methods are underscore prefixed */
    }
};