nullobject

0.7.22 • Public • Published

NullObject

(Javascript) A NullObject has no properties, no constructor and no prototype.

Why?

Ordinary objects in Javascript are already part of an inheritance chain. Consider the following.

var empty = {}
console.log(empty.constructor) // [Function: Object]

Every time you create a new object via an object literal (the {}), behind the scenes JavaScript invokes the Object constructor to create the object, just as if you'd used new Object(). This is what allows your new object to inherit properties from Object.prototype.

But sometimes it would be convenient to create an object that doesn't inherit from a prototype at all and is truly without properties.

var NullObject = require('nullobject')
var empty = new NullObject()

When you console.log something in Javascript, internally it will automatically call toString() on it. A NullObject has 1 non-enumerable toString() method that returns 'NullObject'.

console.log(empty)              // NullObject {}

console.log(empty.constructor)  // undefined
console.log(empty.prototype)    // undefined
console.log(Object.keys(empty)) // []

Crazyness

If you want, you could overwrite the global Object and replace it with NullObject.

const Object = require('nullobject')
global.Object = Object

/* none of the Object methods work!
 * Object.defineProperty() // fails!
 * Object.getOwnPropertyNames() // fails!
 * etc...
 */

Package Sidebar

Install

npm i nullobject

Weekly Downloads

2

Version

0.7.22

License

ISC

Last publish

Collaborators

  • jochemstoel