nil
nil is nothing. falsey. nada. zero. zip. It's a nothing thing that will eat everything you throw at it and return itself.
installation
Installation via npm is the best route. Bindings for OS X and Windows are included.
npm install nil
You can also get those bindings here on github. That folder needs to be in the root nil directory.
it does.. what?
It does a lot of nothing
var nil = nil;!nil // is falsey in boolean comparisonsnil == null // in null/undefined equality classtypeof nil == 'undefined' // type is undefinednil !== undefined // isn't undefined thoughnil === nilalwaysreturnsnil // all properties return nilnil === // returns nil when callednil+'' == '' // returns empty string when coerced to stringObjectprototypetoString // '[object Nil]'Objectlength == 0 // returns empty array when enumeratedObject // null
toString
In order to support coercion to empty string (instead of 'undefined') nil.toString does return a function.
var nilToString = niltoString;!nilToString === true // is NOT falseytypeof nilToString == 'function' // is type function == '' // returns empty stringObject === nil // inherits from nilnilToString === nilToStringcall // call property returns selfnilToString === nilToStringapply // apply property returns selfnilToString === nilToStringbind // bind property returns selfnilToString === nilToStringtoString // toString property returns selfnilToStringanyotherproperty === nil // because it inherits from nilObjectlength == 0 // no enumerable keysObject == 3 // ['call', 'apply', 'bind']
nilWrap and recursiveNilWrap
If run node with the additional flag --harmony
(as in node --harmony yourmodule
) two additional features are exported. The purpose of both of these is to create or wrap existing objects and cause them to return nil any place they would usually return undefined
. The difference between the two is that nilWrap
only wraps the given object, while recursiveNilWrap
will wrap all non-primitive values it returns. The latter essentially allows changing the mechanics of JS at large, when accessing things through the portal of nil.
// the basic version is useful for wrapping prototypes and creating nil-returning classesvar nilWrap = nilWrap; {}NilObjectprototype = ; var test = ;testwhatever = 'some value';console; // 'some value'console; // 'undefined' (nil)
var recursiveNilWrap = recursiveNilWrap;var _global = ;console; // 'undefined' (nil)console; // 'undefined' (nil)console; // {}
notes
ToNumber
Because nil is typeof === 'undefined'
coercion to number is unfortunately NaN
. ES5 spec does not defer to valueOf
to coerce undefined
to a number. V8 appears to follow the spec so it seems impossible to influence the outcome of this coercion. Therefore nil
isn't useful in math operations. The following is useful for this problem (with or without nil);
{ return ? n : 0;}
isObject
Common isObject
functions will fail check for nil. This is probably desirable usually, but if not (like in the case of document.all), an alternative can work.
{ return Objecto === o;} { return o != null && typeof o === 'object' || typeof o === 'function';} { return o == null ? o !== null && o !== undefined : typeof o === 'object' || typeof o === 'function';} // false // false // true