Javascript-Protected methods
A typical implementation of a javascript protected scope blends some of the features: public and private methods and is the hardest scope to reproduce in JavaScript. The two important features of a protected scope(methods), in my estimation, are 1.protected value must be shared across all layers in the prototype chain. 2. Protected values are must not be allowed from outside of the object.
Putting a protected value in the public method is a poor solution because it would not place any limits on accessing that value:
var object = { _notProtected: "I'm a public property!" };
Putting a protected scope is also a poor solution because it would not accessible sub-classes or parent-classes to access the value:
class Database {
constructor() {
const authentication = "I'm a private property!";
this.connect = function() {
alert('I can access ' + authentication);
};
}
}
class CoolDatabase extends Database {
constructor() {
this.connect = function() {
alert("I can't access [authentication] at all! :(");
};
}
connect() {
alert("I also can't access [authentication] #foiledagain");
}
}
This problem would accessible any method within the same object’s prototype chain to access a value, while also denying access by any other object. In JavaScript, that’s a pretty tall order.
