js原型继承与多态 How to apply virtual function in javascript

时间:2024-11-06 10:33:32
function BaseClass() {
this.hello = function() {
this.talk();
}
this.talk = function() {
document.write("I'm BaseClass</br>");
}
}; function MyClass() {
BaseClass.call(this); this.talk = function() {
document.write("I'm MyClass</br>");
} }; MyClass.prototype = new MyClass(); var a = new MyClass();
a.hello();// a is a instance of Child class, the result should be I'm MyClass var b= new BaseClass();
b.hello();// b is a instance of Parent class, the result here should be : I'm BaseClass