The null object which is *actually* an object

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

While reading , 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:

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. what it means to have an empty object
  2. what this demonstrates about prototypal inheritance
  3. Is there a use case for Object.create(null) or is this just something interesting in JS, but we don’t have to know about Object.create in depth because we would never do this in practice, and a template literal object definition whose prototype is Object would always be a lot more useful in practice? I saw one article talking about how if using an object as a map, having it be emptied of the default Object properties could prevent problems (which might occur if a key in the map had the same name as a default Object property)
  4. Personally I feel like I can see some benefit to creating simple objects that don’t inherit any properties from Object, but MDN spent a lot of time covering bugs that can arise from using Object.create(null)
  5. How does prototypal inheritance connect to React? Is this why that dude at Roman says Class components don’t fly and it’s better to use functional components + hooks instead of Redux etc?
  6. Let’s look at some prototype-related methods under the hood:
  7. constructor
  8. factory functions

--

--

--

Software Engineer and Chinese Language Nerd

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Performance insurance

It’s all about RxJS Subject

React basic concepts

Three reasons to switch to Visual Studio 2019 from 2017

Single Page Application deployment pattern in AKS using Azure Devops pipeline — part 2

The easiest way to set up server-side rendering with React and axios

Use a decorator to implement Pub/Sub in a React Application

Angular vs React.js in Enterprise Level Application

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
Theo Carney

Theo Carney

Software Engineer and Chinese Language Nerd

More from Medium

How My Background as a Pianist has Impacted My Coding Ability

Relearning to code the right way

The Art Of Programming #1

That’s what it was? Are you for real?!