how to call parent constructor?

后端 未结 5 501
感动是毒
感动是毒 2020-12-23 17:20

Let\'s say I have the following code snippet.

function test(id) { alert(id); }

testChild.prototype = new test();

function testChild(){}

var instance = new         


        
5条回答
  •  孤独总比滥情好
    2020-12-23 17:39

    By taking advantage of variable arguments and the apply() method, you could do it this way. Here's a fiddle for this example.

    function test(id) { alert(id); }
    function testChild() {
      testChild.prototype.apply(this, arguments);
      alert('also doing my own stuff');
    }
    testChild.prototype = test;
    var instance = new testChild('hi', 'unused', 'optional', 'args');
    

提交回复
热议问题