is there a way in JavaScript to inherit private members from a base class to a sub class?
I want to achieve something like this:
function BaseClass() {
var privateProperty = "private";
this.publicProperty = "public";
}
SubClass.prototype = new BaseClass();
SubClass.prototype.constructor = SubClass;
function SubClass() {
alert( this.publicProperty ); // This works perfectly well
alert( this.privateProperty ); // This doesn't work, because the property is not inherited
}
How can I achieve a class-like simulation, like in other oop-languages (eg. C++) where I can inherit private (protected) pr开发者_StackOverflow中文版operties?
Thank you, David Schreiber
Using Douglas Crockfords power constructor pattern (link is to a video), you can achieve protected variables like this:
function baseclass(secret) {
secret = secret || {};
secret.privateProperty = "private";
return {
publicProperty: "public"
};
}
function subclass() {
var secret = {}, self = baseclass(secret);
alert(self.publicProperty);
alert(secret.privateProperty);
return self;
}
Note: With the power constructor pattern, you don't use new
. Instead, just say var new_object = subclass();
.
Mark your private variables with some kind of markup like a leading underscore _ This way you know it's a private variable (although technically it isn't)
this._privateProperty = "private";
alert( this._privateProperty )
This isn't possible. And that isn't really a private property - it's simply a regular variable that's only available in the scope in which it was defined.
That can't be done, but you could delete the property from the class prototype so that it is not inherited:
SubClass.prototype.privateProperty = undefined;
That way it won't be inherited, but you need to do that for every "private" property in your base class.
Just for reference if someone finds that today (October 2019)
We can implement private properties in javascript using WeakMap()
const _privateProperty = new WeakMap();
class BaseClass {
constructor(){
this.publicProperty = 'public';
_privateProperty.set(this, 'private');
}
}
module.exports = BaseClass;
Inherits the BaseClass
const BaseClass = require('./BaseClass')
class ChildClass extends BaseClass{
constructor(){
super()
}
}
This way your child class will inherit all the public properties from the BaseClass but the private ones.
Now, I am not sure whether one should take this approach but you can read the private properties from your parent class through your child class this way:
const _privateProperty = new WeakMap();
class BaseClass {
constructor(){
this.publicProperty = 'public';
_privateProperty.set(this, 'private');
}
//Public method
readProperties(){
const property.private = _privateProperty.get(this);
return property;
}
}
module.exports = BaseClass;
Child Class
const BaseClass = require('./BaseClass')
class ChildClass extends BaseClass{
constructor(){
super()
}
//Public Method
showProperties(){
super.readProperties().private
}
}
const properties = new ChildClass()
properties.showProperties()
精彩评论