除了我在下面做的事情之外,还有其他方法可以在调用基函数并继承其构造函数的同时覆盖原型函数吗?
function MyFoo(name) {
this.name = name;
}
MyFoo.prototype.sayName = function() {
alert("Hi, my name is " + this.name + ".");
}
MyBar.constructor = MyFoo;
MyBar.prototype = new MyFoo();
function MyBar(name) {
MyFoo.apply(this, arguments);
};
MyBar.prototype.sayName = function() {
MyFoo.prototype.sayName.call(this);
alert("It's nice to meet you!");
}
var fooJim = new MyFoo("Jim");
var barJake = new MyBar("Jake");
fooJim .sayName();
barJake .sayName();
这工作正常,但我只是好奇是否有更有效的方法来做到这一点。