The null object which is *actually* an object

https://en.dopl3r.com/memes/dank/when-you-feel-empty-inside-but-you-cant-express-it-cause-you-dont-know-any-words/513541

Disclaimer: rushed post tonight — xmas eve and all — learn from me at your own risk.

While reading Why Prototypal Inheritance Matters by Aadit M Shah, I had to pause on this variable declaration when I read it in:

let emptyObject = Object.create(null)
// using Object.create(null) yields an object with zero properties; if you’re relatively new to JS, you may have never seen this before. Test it in your console and see what that looks like.

I said to myself something like “lol, but ‘null’ isn’t an object, even though typeof null returns ‘object’; that can’t be right.” However, upon further inspection, sure enough, the Javascript overlords have defined this as a legitimate move in JS:

MDN describes Object.create() thus:

Syntax

Object.create(proto, [propertiesObject])

Parameters

proto

The object which should be the prototype of the newly-created object.

propertiesObject Optional

…(I didn’t include this because it’s not relevant right now)

Exceptions

The proto parameter has to be either

null or

an Object excluding primitive wrapper objects.

Ok, so I’m fine with this: we want a way to create an object that doesn’t inherit from the Object prototype, so even though we’re passing as an argument something that’s NOT an object to a parameter which otherwise takes only object arguments, since this is real the “proto” parameter and the end of the JS prototype chain is “null,” this makes sense from a definitional perspective, if not from a “keep the data-type of your arguments consistent” perspective.

We are creating an object with a prototype of null, similar to Object, only this one doesn’t have the built-in JS object properties. Put another way, in the hierarchy of prototype inheritance, we can sort of think about Object and Object.create(null) as being at the same level of hierarchy: above them in the prototype chain is only null, which is essentially nothing, the end of the line.

Further Topics to explore

  1. Object.create vs. Object.assign

Software Engineer and Chinese Language Nerd

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store