A T*_*A T 5 javascript arguments argument-passing splat ecmascript-5
如何在不使用ECMA6 功能的情况下跨对象进行喷射?
function can(arg0, arg1) {
return arg0 + arg1;
}
function foo(bar, haz) {
this.bar = bar;
this.haz = haz;
}
myArgs = [1,2];
Run Code Online (Sandbox Code Playgroud)
我可以这样can做:
can.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)
尝试使用时foo:
new foo.apply(this, myArgs);
Run Code Online (Sandbox Code Playgroud)
我收到此错误(因为我正在调用new):
TypeError: function apply() { [native code] } is not a constructor
Run Code Online (Sandbox Code Playgroud)
Object.createfunction 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
578 次 |
| 最近记录: |