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

 

 

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

 

你可能感兴趣的:(JavaScript)