little bits of gold I picked up here and there everyday
Aug 24, 2015 node docs node version

When using the cluster module to create child processes, make sure which node version you are using. In v0.10.x setupMaster can only be called once and subsequent calls are ignored, while in v0.12.x one can call setupMaster multiple times and assign different tasks to workers.

Important: read the right docs for your node version.

Any object that has indexed properties and an appropriate length property is an array-like object, on which generic Array methods can be used by extracting method objects and using their call method.

Subclassing in Javascript:

  • call the superclass constructor explicitly from the subclass constructor, passing this as the explicit receiver
function Subfoo(arg) {, arg);
  • use Object.create to construct the subclass prototype object to avoid calling superclass constructor.
Subfoo.prototype = Object.create(Foo.prototype);
  • Never reuse a superclass property name in a subclass, because the property is stored on instance objects and named with a string. Reusing the same name refers to the same property.

  • Inheriting from the standard classes tends to break due to special internal properties such as [[class]]. Prefer delegating to properties instead of inheriting from standard classes.

High order functions are functions that take other functions as argument or return functions as their result.

Difference between method, function and constructor calls:

  • Method calls provide the object in which the method property is looked up as their receiver;

  • Function calls provide the global object as their receiver;

  • Constructors called with new provide a new object as their receiver.

Indirect eval call (0, eval)(arg) loses access to any local scope, and the argument is evaluated in global scope, while direct eval call eval(arg) have access to full scope at the point where it is called. Direct eval call needs scope available at run time, hence is inefficient and insecure. Prefer indirect eval call to direct eval call.

// indirect eval call
var foo = eval;
// the equivalent shorter expression
(0, eval)(bar);

Different ways of defining a function in JavaScript:

  • Function declaration:
function foo(x) {...}
  • Anonymous function expression:
var foo = function(x) {...}
  • Named function expression:
// useful for debugging, try to avoid otherwise
var foo = function bar(x) {...}
// bar is only available inside the scope of function foo, not defined in the outer scope

Variable declarations in a block are implicitly hoisted to the top of the enclosing function, any redeclarations of the same variable has the effect of a single variable declaration. Sometimes it's better to manually hoist local variable declarations to avoid confusions.

The same rule applies to function declarations. Note that JavaScript is not block-scoped, which means a function/variable declared in a enclosing block, e.g. if block, will be hoisted to the top of the enclosing function, and therefore is defined in that function scope.

Facts about JavaScript closures:

  • Functions can refer to variables defined in outer scopes;

  • Closures can outlive the function that creates them;

  • Closures internally store references to their outer variables, and can both read and update their stored variables.

  • Closures store their outer variables by reference, not value

When "==" is used for comparison, implicit coercions are applied on the arguments that are of different types. Always prefer strict equality operator "===" and make explicit coercion when comparing values of different types.