具有可变参数的ES6类super() [英] ES6 class super() with variadic arguments

查看:176
本文介绍了具有可变参数的ES6类super()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES6中,是否有一种方法可以调用通过可变参数传递的父构造函数,一个 foo.apply(this,arguments)?我已经找到一个答案,唯一的例子是调用 super()(无参数)或调用 super(x,y )(具体的参数)。 super.apply(this,arguments)似乎没有起作用。

解决方案

我找到方便的模式是

 构造函数(... args){
super(。 ..args);
}

如果您拥有并使用命名参数,可以改为: p>

 构造函数(a,b,c){
super(... arguments);
}

参考文献:




In ES6, is there a way to call a parent constructor passing through variadic arguments, a la foo.apply(this, arguments)? I've looked for an answer, and the only instances I see are either calling super() (no arguments) or calling super(x, y) (with specific arguments). super.apply(this, arguments) doesn't appear to work.

解决方案

The pattern I find convenient and follow is

constructor(...args) {
    super(...args);
}

In case you have and use named arguments you could do this instead:

constructor(a, b, c) {
    super(...arguments);
}

References:

这篇关于具有可变参数的ES6类super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆