Member-only story

What Is Variable Privacy?

It’s possible that you’ve never heard of the idea of variable or method privacy before. Yet, it is encountered so often as part of feature set in many computer languages, not just JavaScript.

Ghost Together
5 min readMar 4, 2019

A common running theme in software development is variable privacy. This is rarely taught because it’s more of a principle than a language feature.

In C++ you even have a special keyword called: private, that limits variable use to a particular class. In JavaScript… we don’t have private keyword.

But it doesn’t mean variable or method privacy are impossible to achieve with its current set of features. In fact, if you start paying attention, you will notice that the idea is omnipresent even in subtle language features:

console.log(x); // reference error
{
let x = 1;
}

Here x is private to the scope in which it was defined. You cannot access it outside of that scope, without generating an error. It is often taught that this is the difference between var and let.

I guess you can see how it works. But wait. Why?

Well, because var used to hoist the definition. And automatically shoving things into global scope isn’t good coding practice.

It is incredible how many times this privacy effect is observed, not just with let keyword alone but in many…

--

--

Ghost Together
Ghost Together

Written by Ghost Together

Ghost Together @ https://semicolon.dev is an alternative to Twitter. Sign up to meet other makers of things.

No responses yet