Introduction
In order to get the properties of a class instance we can just use the Object.getOwnPropertyNames()
method like in the following example
class People {
constructor(name, age) {
this.name = name;
this.age = age;
}
getProperties() {
return Object.getOwnPropertyNames(this);
}
}
console.log(new People("John", 20).getProperties())
This does work because the properties of the object are public. But in case the properties of the object are private this method doesn't work.
Problem
Imagine that you don't want user to modify the name
and age
properties directly outside a class method, so you make them private. However, you also want to have a method that makes a copy of an instance in another instance. Since the Object.getOwnPropertyNames()
does not work for private properties, you cannot access to the keys in oder to make the clone()
:
class People {
#name; #age;
constructor(name, age) {
this.#name = name;
this.#age = age;
}
clone(o) {
Object.getOwnPropertyNames(this).forEach(key => this[key] = o[key]);
}
getName() { return this.#name }
}
p1 = new People("John", 20);
p2 = new People("", 0);
p2.clone(p1); // Now p2 should have the same properties of p1
console.log(p2.getName()); // Prints "" and should print "John"
Question
Is there a way to access private properties names of a class from inside a class method?
Yes. You can use something called
WeakMaps
. You would do something like this:As you can see, we use the
set
method to set the value of a weakMap for a specific class.Now, to access this value in another method, you use the
get
method:The
get
method will allow you to access the value of that weakMap for the class mentioned.For more information about WeakMaps click here