如何解决将 .apply() 与“new”运算符一起使用这可能吗?
使用 ECMAScript5
的Function.prototype.bind
东西变得非常干净:
function newCall(Cls) {
return new (Function.prototype.bind.apply(Cls, arguments));
// or even
// return new (Cls.bind.apply(Cls, arguments));
// if you know that Cls.bind has not been overwritten
}
它可以按如下方式使用:
var s = newCall(Something, a, b, c);
甚至直接:
var s = new (Function.prototype.bind.call(Something, null, a, b, c));
var s = new (Function.prototype.bind.apply(Something, [null, a, b, c]));
这个和基于 eval
的解决方案是唯一始终有效的解决方案,即使使用特殊的构造函数,例如Date
:
var date = newCall(Date, 2012, 1);
console.log(date instanceof Date); // true
一点解释:我们需要new
在一个接受有限数量参数的函数上运行。该bind
方法允许我们这样做:
var f = Cls.bind(anything, arg1, arg2, ...);
result = new f();
该anything
参数无关紧要,因为new
关键字
resetf
的上下文。但是,出于语法原因,它是必需的。现在,对于bind
调用:我们需要传递可变数量的参数,所以这可以解决问题:
var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]);
result = new f();
让我们将其包装在一个函数中。Cls
作为参数 0 传递,所以它将是我们的anything
.
function newCall(Cls /*, arg1, arg2, ... */) {
var f = Cls.bind.apply(Cls, arguments);
return new f();
}
f
实际上,根本不需要临时变量:
function newCall(Cls /*, arg1, arg2, ... */) {
return new (Cls.bind.apply(Cls, arguments))();
}
最后,我们应该确保这bind
确实是我们需要的。(Cls.bind
可能已被覆盖)。所以用
替换它Function.prototype.bind
,我们得到如上的最终结果。
解决方法
在 JavaScript 中,我想创建一个对象实例(通过new
运算符),但将任意数量的参数传递给构造函数。这可能吗?
我想做的是这样的(但下面的代码不起作用):
function Something(){
// init stuff
}
function createSomething(){
return new Something.apply(null,arguments);
}
var s = createSomething(a,b,c); // 's' is an instance of Something
答案
从这里的响应中可以清楚地看出,没有内置的方式可以.apply()
与new
操作员通话。然而,人们提出了一些非常有趣的解决方案来解决这个问题。
我首选的解决方案是来自 Matthew Crumley的解决方案(我已对其进行了修改以传递arguments
属性):
var createSomething = (function() {
function F(args) {
return Something.apply(this,args);
}
F.prototype = Something.prototype;
return function() {
return new F(arguments);
}
})();