js原型繼承與多型 How to apply virtual function in javascript

zyip發表於2015-03-06

 

 

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

 

相關文章