Hiding properties in JS… for real

Sometimes, you want to hide the property of an object1. Probably you are a library author and you are afraid that:

  • your users will see it and attempt to use it
  • your users will try to enumerate over it although it might not make sense

So here is what you do:

Hoisting in JavaScript

This behavior can surprise beginners to JavaScript, and also, I admit, people like me who learned JavaScript by doing.
So what is hoisting about? Concisely put, it means that every variable declared in a scope is actually declared when entering the scope, no matter where you choose to put the declaration.

In the following examples I’m going to create a function to define a scope, but this can be applied to every scope, the global scope, a scope you define with { and }, etc.