Javascript特权函数继承
在过去的几周里,我对Javascript继承做了大量的研究。首先,我想说的是,我并没有试图在Javascript中实现基于类的继承,但是,我试图使用Javascript的原型特性正确地模拟继承。这可能是白费力气,但我的问题是: 我有两个简单的函数:Javascript特权函数继承,javascript,inheritance,prototype,Javascript,Inheritance,Prototype,在过去的几周里,我对Javascript继承做了大量的研究。首先,我想说的是,我并没有试图在Javascript中实现基于类的继承,但是,我试图使用Javascript的原型特性正确地模拟继承。这可能是白费力气,但我的问题是: 我有两个简单的函数: function Animal(arg) { var a = arg; //privileged function has access to private variable this.sayHi = function(n
function Animal(arg) {
var a = arg;
//privileged function has access to private variable
this.sayHi = function(name) {
// can access a here if needed
console.log('Hi: ' + name);
}
}
以及:
预期结果如下:
var animal = new Animal('cow');
animal.sayHi('John'); // Hi: John
var person = new Person('adult');
person.sayHi('Fred'); // Hi: Fred!!!
我用于继承的“inheritsFrom()”方法如下所示:
Function.prototype.inheritsFrom = function(parent) {
if(parent.constructor == Function) { //Normal Inheritance
this.prototype = new parent;
this.prototype.constructor = this;
this.prototype.parent = parent.prototype;
} else { //Pure Virtual Inheritance
this.prototype = parent;
this.prototype.constructor = this;
this.prototype.parent = parent;
}
return this;
}
以下是我的一些参考资料:
有很多好的信息,但仍然无法理解如何继承如上所示的“特权”方法。我能够调用父类的特权方法。请参阅以下JS Fiddle:
非常感谢您对继承特权方法的指导,谢谢 这个问题没有很好的解决办法。首先
this.prototype = new parent;
这不是建立继承的好方法。宁可使用
this.prototype = Object.create(parent.prototype);
有关更多信息,请参阅
在子构造函数中,必须调用父构造函数,并将其应用于当前实例:
function Person(arg) {
Animal.call(this, arg);
}
然后,您必须迭代由Animal
附加到this
的所有方法,并保留对它们的引用:
function Person(arg) {
Animal.call(this, arg);
var privilegedSuper = Object.keys(this)
.filter(function(prop) { return typeof this[prop] === 'function'; }.bind(this))
.reduce(function(obj, prop) { return (obj[prop] = this[prop]), obj; }.bind(this));
}
然后您可以在重写的方法中引用它:
function Person(arg) {
Animal.call(this, arg);
var privilegedSuper = Object.keys(this)
.filter(function(prop) { return typeof this[prop] === 'function'; }.bind(this))
.reduce(function(obj, prop) { return (obj[prop] = this[prop]), obj; }.bind(this));
this.sayHi = function(name) {
privilegedSuper.sayHi.call(this, name + '!!!');
};
}
它值得付出努力和复杂性吗?我不这么认为
Function.prototype.inheritsFrom=函数(父级){
if(parent.constructor==函数){//正常继承
this.prototype=Object.create(parent.prototype);
this.prototype.constructor=this;
this.prototype.parent=parent.prototype;
}else{//纯虚拟继承
this.prototype=parent;
this.prototype.constructor=this;
this.prototype.parent=parent;
}
归还这个;
}
功能动物(arg){
var a=arg;
//特权函数可以访问私有变量
this.sayHi=函数(名称){
//如果需要,您可以在此访问
log('Hi:'+name+'(这里是一个:'+a+'));
}
}
职能人员(arg){
动物。呼叫(这个,arg);
var privilegedSuper=Object.keys(此)
.过滤器(功能(道具){
此[prop]=='function'的返回类型;
}.绑定(本)
.减少(功能(obj、prop){
返回(obj[prop]=此[prop]),obj;
}.bind(这个),{});
this.sayHi=函数(名称){
privilegedSuper.sayHi.call(this,name+'!!!);
};
}
从(动物)继承的人;
var animal=新动物(“奶牛”);
动物。sayHi('John');//嗨:约翰
var person=新人(“成年人”);
person.sayHi('Fred');//嗨:弗雷德是的,你在打一匹死马。这是JavaScript—在ES7成为标准之前,您必须按照当前设计的实现方式使用它—使用原型继承而不是经典继承。非常聪明。请注意,.filter(function(prop){…}.bind(this))
可能是.filter(function(prop){…},this)
,这可能更有效,但我想它保持了与reduce的样式一致,reduce没有thisArg参数。@RobG:理想情况下,我会使用arrow函数;)菲利克斯,反应很好,谢谢!有趣的是,我开始研究通过特权方法循环并将它们附加到变量的确切实现。。。但后来我觉得我疯了,一定有更好的办法,这就是为什么我在网上这么贴。由于解释和代码示例的清晰性,我将此标记为可接受的答案,但是我认为我正在远离特权方法,而是将所有内容附加到对象原型上。。。只需要将所有私有变量公开,以便原型可以访问它们。@ossys:是的,这将是最好的解决方案。我不是JS中“特权方法”的拥护者。它只是引入了不必要的复杂性。
function Person(arg) {
Animal.call(this, arg);
var privilegedSuper = Object.keys(this)
.filter(function(prop) { return typeof this[prop] === 'function'; }.bind(this))
.reduce(function(obj, prop) { return (obj[prop] = this[prop]), obj; }.bind(this));
this.sayHi = function(name) {
privilegedSuper.sayHi.call(this, name + '!!!');
};
}