【发布时间】:2015-07-29 20:42:10
【问题描述】:
我需要 Javascript 中两个类之间的继承关系。 我喜欢在构造函数中声明属性;对于方法,原型:
function Animal(){
this.someProperty = 'someProperty';
this.init();
}
Animal.prototype = {
init : function(){ }
anotherMethod : function(){ }
}
我认为声明这样的方法比:
Animal.prototype.init = function(){ }
Animal.prototype.anotherMethod = function(){}
但是当我需要从另一个类继承某个类时,我找不到按照我的方式去做的方法。这不起作用:
Cat.prototype = new Animal();
Cat.prototype = {
init : function(){ }
}
我知道我可以像下一个那样做:
Cat.prototype = new Animal();
Cat.prototype.init = function(){ }
Cat.prototype.anotherMethod = function(){ }
但是,有没有办法按照我的方式去做?
【问题讨论】:
-
我想你会喜欢新的 node.js EC6 类功能developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript oop inheritance