JavaScript’s Journey To Privacy

It Sucks Without Privacy

function SomeConstructor() {
this._privateProp = 'don\'t touch this';
this.publicProp = 'you can touch this';
}
function SomeConstructor() {
const privateProp = 'dont touch this';
this.publicProp = 'you can touch this';
this.doSomethingWithPrivateProp = () => { ... }
}
function SomeModule() {
const privateProp = 'dont touch this';
const publicProp = 'you can touch this';
_doSomethingWithPrivateProp = () => { ... } const publicMethod = () => {
_doSomethingWithPrivateProp();
// ...
}
return {
publicProp,
publicMethod
}
}
const privateMethodName = Symbol();
const privatePropName = Symbol();
class SomeClass {
[privatePropName] = 'dont touch this';;
publicProp = 'you can touch this';
[privateMethodName]() {
console.log('private method');
}
publicMethod() {
this[privateMethodName](this[privatePropName]);
}
}

Some Good Hash (#)

  • Specs in the Stage-3 will most likely make it to the standarized specs unless there is a significant reason for dismissal. However, it can still be changed or improved.
  • The private keyword will not be used. Instead, the hash # prefix will be used. It is not a keyword; it is a prefix. By prefixing the property name with a hash, you can make the property private.
  • It is part of the Class Field Declarations specs. The difference between the private and the public fields is that the private field can only be made through field declaration. In other words, the private fields cannot be added to objects dynamically.
  • Methods are limited. It cannot be used as methods declarations. You can make private methods through proper function representations. This is only currently speaking. Specs may be updated. (Class fields and private methods: Stage 3 update)
  • Computed Property Names are not allowed. Only #foo itself is recognized as private, and #[fooname] will throw a syntax error.
  • All private fields will have its own unique scope within the located class. Therefore, it comes with interesting characteristics that we will cover later.
class Human {
#age = 10;
}
const person = new Human();
console.log(person.#age); // Error TS18013: Property '#age' is not accessible outside class 'Human' because it has a private identifier.
class Human {
#age = 10;
getAge() {
return this.age; // Error TS2551: Property 'age' does not exist on type 'Human'. Did you mean '#age'?
}
}
class Human {
#age = 10;
getAge() {
return this.#age;
}
}
const person = new Human();console.log(person.getAge()); // 10
class Human {
#age = 10;
getAge() {
return this.#age;
}
}
class Person extends Human {
getFakeAge() {
return this.#age - 3; // Property '#age' is not accessible outside class 'Human' because it has a private identifier.
}
}
class Human {
age = 10;
getAge() {
return this.age;
}
}
class Person extends Human {
age = 20;
getFakeAge() {
return this.age;
}
}
const p = new Person();
console.log(p.getAge()); // 20
console.log(p.getFakeAge()); // 20
class Human {
#age = 10;
getAge() {
return this.#age;
}
}
class Person extends Human {
#age = 20;
getFakeAge() {
return this.#age;
}
}
const p = new Person();
console.log(p.getAge()); // 10
console.log(p.getFakeAge()); // 20

Closing Remarks

--

--

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