Small, portable, idioms

This is the standard "data type" pattern:

function DataType(options) {
  // Ctor guard, if the class is exported.
  if (!(this instanceof DataType))
    return new DataType(options);

  // If inheriting:
  // (explicitly naming the superclass is common, albeit a slight DRY
  // violation)
  Base.call(this, options);

  // Add some fields.
  this._privateArray = []
  this.name = 'foo';
}

DataType.prototype = Object.create(Base.prototype, {
  constructor: { value: DataType }
});

DataType.prototype.someMethod = function () {
  console.log('something');
}
DataType.prototype._somePrivateMethod = function () {
  console.log('something');
}


// Generate some random hex digits.
Math.random().toString(16).slice(2)


This the spacing is kind of weird for this, but after looking at it a bit
it actually seems pretty readable. There's no "hanging indentation" in the
condition that obscures the indent of the body.
    if (buf && typeof buf === 'object'
    && buf.constructor && buf.constructor.name === 'Buffer'
    && buf.length
    && typeof buf.slice === 'function') {
        // treat like a buffer
        if (!self._bufs) self._bufs = [];

Small, portable, idioms

This is the standard "data type" pattern:

function DataType(options) {
  // Ctor guard, if the class is exported.
  if (!(this instanceof DataType))
    return new DataType(options);

  // If inheriting:
  // (explicitly naming the superclass is common, albeit a slight DRY
  // violation)
  Base.call(this, options);

  // Add some fields.
  this._privateArray = []
  this.name = 'foo';
}

DataType.prototype = Object.create(Base.prototype, {
  constructor: { value: DataType }
});

DataType.prototype.someMethod = function () {
  console.log('something');
}
DataType.prototype._somePrivateMethod = function () {
  console.log('something');
}


// Generate some random hex digits.
Math.random().toString(16).slice(2)


This the spacing is kind of weird for this, but after looking at it a bit
it actually seems pretty readable. There's no "hanging indentation" in the
condition that obscures the indent of the body.
    if (buf && typeof buf === 'object'
    && buf.constructor && buf.constructor.name === 'Buffer'
    && buf.length
    && typeof buf.slice === 'function') {
        // treat like a buffer
        if (!self._bufs) self._bufs = [];



This is basically like "a contains b". I find this really cryptic, but a
lot of people use it, and I feel like I should write it down so that I
don't forget it. It just has this "too clever" vibe that sets off my
programmer alarms.
~a.indexOf(b)

Make note of annoying pattern.
Make note of annoying pattern.
