Here is a regular constructor function:
var Hello = function (name) {
this.name = name;
};
When used with new, you get what you would expect:
var dt = (function () { | |
var start = Date.now(); | |
return function () { | |
return Date.now() - start; | |
}; | |
})(); | |
// Some time later... |
/* This script operates asynchronously. It can be written synchronously, but | |
* not a lot is saved once you try-catch everything. Uses the async module. | |
* | |
* MIT licence. | |
*/ | |
var fs = require('fs'); | |
var path = require('path'); | |
var async = require('async'); | |
// Write as many of these as you like. |
Here is a regular constructor function:
var Hello = function (name) {
this.name = name;
};
When used with new, you get what you would expect:
var async = require('async'); | |
// ASYNC SWITCH patches the async module with a switch like construct for asynchronous functions. | |
// Use like: | |
// async.switch(sitchedVar, { | |
// 'case 1': function (callback) {/*...*/}, | |
// 'case 2': function (callback) {/*...*/}, | |
// 'default': function (callback) {/*...*/} | |
// }, callback); |
// Package unshortener takes a single parameter (url) and sends back an unshortened version. | |
// | |
// This version is in vanilla go, and requires no packages to be installed. | |
package main | |
import ( | |
"encoding/json" | |
"io" | |
"log" | |
"net/http" |
Well written code by several authors should look like well written code by one author. To this end, everyone should write code in the same style and formatting. This serves multiple purposes, and it should be pretty clear what the benefits of consistent style with no arguments are.
This is the guide for the Entrago CMS backend codebase.
function nest(before, after) { | |
'use strict'; | |
if (typeof before !== 'function') { | |
// Only after is defined, so just return it. | |
if (typeof after === 'function') { | |
return after; | |
} | |
// Neither function is defined, so return a function that just executes a callback. |
/** | |
* Mark S Everitt 2013 | |
* Licence: MIT | |
*/ | |
var jshintConfig = { | |
files: ['gruntfile.js', 'lib/**/*.js', 'test/**/*.js', 'index.js'], | |
options: { | |
// Just an example. | |
globals: { |
// All angles in radians. | |
function haversine(aLong, aLat, bLong, bLat) { | |
var R = 6371; // Radius of Earth. | |
var latDiff = bLat - aLat; | |
var longDiff = bLong - aLong; | |
var step1 = Math.sin(latDiff / 2); | |
var step2 = Math.sin(longDiff / 2); | |
var step3 = step1 * step1 + step2 * step2 * Math.cos(aLat) * Math.cos(bLat); |
/** | |
* The default JSHint output is somewhat wasteful, and very bland, making it hard for a human to | |
* parse. The following does IMO a better job and colours the output for you. This requires the | |
* `colors` package, available via npm. If you don't want the dependency, just remove the color | |
* commands from strings. | |
* | |
* Usage: When invoking jshint from the command line, point it to this file with the --reporter | |
* flag. e.g. | |
* | |
* jshint someFile.js --config /path/to/config.cfg --reporter /path/to/jshintReporter.js |