js 원형 계승 및 다 중 How to apply virtual function in javascript

1896 단어 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

좋은 웹페이지 즐겨찾기