Parent = (function() {
var parent = function(name) {
this.name = name;
};
parent.prototype.dostuff = function() {
console.log(this.name);
};
parent.prototype.more = function() {
console.log("this should be available on both ... " + this.name);
};
return parent;
}());
Child = (function() {
var child = function(name) {
Parent.call(this, name);
this.prototype = Object.create(Parent.prototype);
};
child.prototype.dostuff = function() {
console.log("the child class ... " + this.name);
};
child.prototype.special = function() {
console.log("only the child has this method");
};
return child;
}());
在上面,我的父对象有一个名为“more”的方法,但是当我像这样新建孩子时......我不能调用 more 方法,除非我手动将其原型化。我在上面遗漏了什么可以让我免费获得原型方法?
var first = new Parent("father");
var last = new Child("son");
使用工作 jsfiddle 更新