图示了JavaScript对象(新)? [英] splat over JavaScript object (with new)?
本文介绍了图示了JavaScript对象(新)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何跨多个对象图示不使用 ECMA6功能的?
尝试
功能可以(为arg0,ARG1){
返回将arg0 + ARG1;
}函数foo(巴,HAZ){
this.bar =栏;
this.haz = HAZ;
}myArgs = [1,2];
使用可
我可以这样做:
can.apply(这一点,myArgs);
当,试图富
:
新foo.apply(这一点,myArgs);
我得到这个错误(因为我打电话新
)
类型错误:函数适用(){[本地code]}不是构造函数
解决方案
使用的Object.create
函数foo(巴,HAZ){
this.bar =栏;
this.haz = HAZ;
}X =的Object.create(foo.prototype);
myArgs = [5,6];
foo.apply(X,myArgs);的console.log(x.bar);
How do I splat across objects without using ECMA6 features?
Attempt
function can(arg0, arg1) {
return arg0 + arg1;
}
function foo(bar, haz) {
this.bar = bar;
this.haz = haz;
}
myArgs = [1,2];
With can
I can just do:
can.apply(this, myArgs);
When trying with foo
:
new foo.apply(this, myArgs);
I get this error (because I'm calling new
):
TypeError: function apply() { [native code] } is not a constructor
解决方案
Using Object.create
function foo(bar, haz) {
this.bar = bar;
this.haz = haz;
}
x = Object.create(foo.prototype);
myArgs = [5,6];
foo.apply(x, myArgs);
console.log(x.bar);
这篇关于图示了JavaScript对象(新)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文