Okay, this question might seem silly but I'm learning object oriented concepts in javascript into more depth so to avoid confusions I had to ask this anyway.
const ObjConstructor = function() {
this.name = 'object';
let value = 1;
this.destroyValue = function() {
value = 0;
}
this.getValue = function() {
return value;
}
}
The above code implements an object constructor function, and it has a private member value
. The way I assume how this works every time an object is created using this constructor is it basically calls the call()
method on ObjConstructor
object and returns the object with members (in this case it is name, destroyValue and getValue) assigned to it.
But I get so confused when understanding how destroyValue
and getValue
methods could be able to access the private variable value
here. Because it is not a member of the current object the methods are called on, and also value
which is a local variable of the constructor function was got destroyed after the first time it executed the constructor function to create the object.