how to call parent constructor?

后端 未结 5 365
感动是毒
感动是毒 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:36

    You already have many answers, but I'll throw in the ES6 way, which IMHO is the new standard way to do this.

    class Parent { 
      constructor() { alert('hi'); } 
    }
    class Child extends Parent { 
      // Optionally include a constructor definition here. Leaving it 
      // out means the parent constructor is automatically invoked.
      constructor() {
        // imagine doing some custom stuff for this derived class
        super();  // explicitly call parent constructor.
      }
    }
    
    // Instantiate one:
    var foo = new Child();  // alert: hi
    
    0 讨论(0)
  • 2020-12-23 17:38

    You need to declare the function testChild() before you set its prototype. Then you need to call testChild.test to call the method. I believe you want to set testChild.prototype.test = test, then you can call testChild.test('hi') and it should resolve properly.

    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-12-23 17:46

    JS OOP ...

    // parent class
    var Test = function(id) {
        console.log(id);
    };
    
    // child class
    var TestChild = function(id) {
        Test.call(this, id); // call parent constructor
    };
    
    // extend from parent class prototype
    TestChild.prototype = Object.create(Test.prototype); // keeps the proto clean
    TestChild.prototype.constructor = TestChild; // repair the inherited constructor
    
    // end-use
    var instance = new TestChild('foo');
    
    0 讨论(0)
  • 2020-12-23 17:57

    That's how you do this in CoffeeScript:

    class Test
      constructor: (id) -> alert(id)
    
    class TestChild extends Test
    
    instance = new TestChild('hi')
    

    Nope, I'm not starting a holy war. Instead, I'm suggesting to take a look at resulting JavaScript code to see how subclassing could be implemented:

    // Function that does subclassing
    var __extends = function(child, parent) {
      for (var key in parent) {
        if (Object.prototype.hasOwnProperty.call(parent, key)) {
          child[key] = parent[key];
        }
      }
      function ctor() { this.constructor = child; }
      ctor.prototype = parent.prototype;
      child.prototype = new ctor;
      child.__super__ = parent.prototype;
      return child;
    };
    
    // Our code
    var Test, TestChild, instance;
    
    Test = function(id) { alert(id); };
    
    TestChild = function() {
      TestChild.__super__.constructor.apply(this, arguments);
    }; __extends(TestChild, Test);
    
    instance = new TestChild('hi');
    
    // And we get an alert
    

    See it in action at http://jsfiddle.net/NGLMW/3/.

    To stay correct, the code is slightly modified and commented to be more readable, compared to CoffeeScript output.

    0 讨论(0)
提交回复
热议问题