function Test() {}
Test.prototype.say = function (word) {
alert(word);
}
Test.prototype.speak = function () {
this.say('hello');
}
Test.prototype.say = function (word) {
console.log(word);
}
最后的分配将覆盖所有 Test 对象的 say 方法。如果要在继承函数(类)中覆盖它:
function Test() {}
Test.prototype.say = function (word) {
alert(word);
}
Test.prototype.speak = function () {
this.say('hello');
}
function TestDerived() {}
TestDerived.prototype = new Test(); // javascript's simplest form of inheritance.
TestDerived.prototype.say = function (word) {
console.log(word);
}
如果您想在特定的测试实例中覆盖它:
function Test() {}
Test.prototype.say = function (word) {
alert(word);
}
Test.prototype.speak = function () {
this.say('hello');
}
var myTest = new Test();
myTest.say = function (word) {
console.log(word);
}