2

2 つのメソッドを持つ親クラスを作成し、子クラスを作成して 1 つの親メソッドをオーバーライドしています。私は以下のようなJavaScriptオブジェクト構造を持っています。

function parent(){
}

parent.prototype = {
    method1:function(){
    this.method2();
    },

    method2:function(){
        console.log("I am in parent.method2()")
    }
}


//inheriting
function child(){
    parent.call(this);
}
child.prototype = new parent();
child.prototype.constructor= parent;

//overriding method2
child.prototype.method2 = function(){
        console.log("I am in child.method2()")
}


//creating child object
var childObj = new child();

childObj.method2() // will call the overriden method

childObj.method1() //here method1 will invoke method2 of parent. 

オーバーライドされたメソッドを常に強制的に呼び出すにはどうすればよいですか?

それとも、何か間違ったことをしようとしていますか?

4

0 に答える 0