Javascript 如何调用父构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6617780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:29:35  来源:igfitidea点击:

how to call parent constructor?

javascriptinheritance

提问by Moon

Let's say I have the following code snippet.

假设我有以下代码片段。

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

testChild.prototype = new test();

function testChild(){}

var instance = new testChild('hi');

Is it possible to get alert('hi')? I get undefinednow.

有可能得到alert('hi')吗?我undefined现在明白了。

采纳答案by Anton Strogonoff

That's how you do this in CoffeeScript:

这就是你在 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:

不,我不会发动圣战。相反,我建议查看生成的 JavaScript 代码,以了解如何实现子类化:

// 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/.

http://jsfiddle.net/NGLMW/3/ 上查看它的实际效果。

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

为保持正确,与 CoffeeScript 输出相比,代码稍作修改和注释以提高可读性。

回答by roylaurie

JS OOP ...

JS面向对象...

// 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');

回答by Sk606

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

您已经有很多答案,但我会采用 ES6 方式,恕我直言,这是执行此操作的新标准方式。

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

回答by JCotton

By taking advantage of variable argumentsand the apply()method, you could do it this way. Here's a fiddlefor this example.

通过利用可变参数apply()方法,您可以这样做。这是此示例的小提琴

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');

回答by Paul Sonier

You need to declare the function testChild()before you set its prototype. Then you need to call testChild.testto 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.

您需要function testChild()在设置其原型之前声明。然后你需要调用testChild.test来调用方法。我相信你想设置testChild.prototype.test = test,然后你可以打电话testChild.test('hi'),它应该能正确解决。