Skip to content

Instantly share code, notes, and snippets.

@joshuaskelly
Last active May 22, 2019 04:46
Show Gist options
  • Save joshuaskelly/9ace3f977972e5c6fa55 to your computer and use it in GitHub Desktop.
Save joshuaskelly/9ace3f977972e5c6fa55 to your computer and use it in GitHub Desktop.
JavaScript Style Cheat Sheet
/**
* JavaScript Style Cheat Sheet
* A short and sweet example of style, patterns, and practices.
*
* @author Joshua Skelton
*/
let Namespace = Namespace || {};
Namespace.Module = Namespace.Module || {};
/**
* A sample class defined in a module contained within
* a namespace.
*
* @namespace Namespace.Module
* @constructor
*
* @param {String} name
*/
Namespace.Module.Class = class {
constructor(name) {
this.name = name;
}
/**
* A simple function taking no parameters.
*
* @returns {String}
*/
foo() {
return this.name;
}
};
/**
* A sample class illustrating inheritance.
*
* @namespace Namespace.Module
* @constructor
*
* @param {String} firstName
* @param {String} lastName
*/
Namespace.Module.SubClass = class extends Namespace.Module.Class {
constructor (firstName, lastName) {
// Invoke the super class's constructor
super(firstName);
this.lastName = lastName;
}
// Override Class foo's function. Make use of both local
// and inherited data members.
foo() {
return this.lastName + ", " + this.name;
}
}
var c = new Namespace.Module.Class("Name");
var sc = new Namespace.Module.SubClass("First", "Last");
console.log(c.foo());
console.log(sc.foo());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment