在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
这个问题的答案
从这里的响应可以清楚地看出,没有内置的方法可以使用new操作符调用.apply()。然而,人们对这个问题提出了许多非常有趣的解决方案。
我更喜欢的解决方案是来自Matthew Crumley的这个(我修改了它来传递arguments属性):
var createSomething = (function() {
function F(args) {
return Something.apply(this, args);
}
F.prototype = Something.prototype;
return function() {
return new F(arguments);
}
})();
假设你有一个Items构造函数,它吸收了你给它的所有参数:
function Items () {
this.elems = [].slice.call(arguments);
}
Items.prototype.sum = function () {
return this.elems.reduce(function (sum, x) { return sum + x }, 0);
};
你可以用Object.create()创建一个实例,然后用.apply()创建该实例:
var items = Object.create(Items.prototype);
Items.apply(items, [ 1, 2, 3, 4 ]);
console.log(items.sum());
当运行时输出10,因为1 + 2 + 3 + 4 == 10:
$ node t.js
10
你为什么要把事情弄得这么复杂。在new之后使用匿名函数,该函数返回带有应用数组的构造函数。
function myConstructor(a,b,c){
this.a = a;
this.b = b;
this.c = c;
}
var newObject = new myConstructor(1,2,3); // {a: 1, b: 2, c: 3}
var myArguments = [1,2,3];
var anotherObject = new function(){
return myConstructor.apply(this,myArguments);
}; // {a: 1, b: 2, c: 3}
改良版的@Matthew的回答。这种形式具有将临时类存储在闭包中所获得的轻微性能优势,以及可以使用一个函数创建任何类的灵活性
var applyCtor = function(){
var tempCtor = function() {};
return function(ctor, args){
tempCtor.prototype = ctor.prototype;
var instance = new tempCtor();
ctor.prototype.constructor.apply(instance,args);
return instance;
}
}();
这将通过调用applyCtor(类,[arg1, arg2, argn])来使用;
修改了@Matthew的答案。在这里,我可以像往常一样传递任意数量的参数给函数(不是数组)。此外,'Something'也没有硬编码:
function createObject( constr ) {
var args = arguments;
var wrapper = function() {
return constr.apply( this, Array.prototype.slice.call(args, 1) );
}
wrapper.prototype = constr.prototype;
return new wrapper();
}
function Something() {
// init stuff
};
var obj1 = createObject( Something, 1, 2, 3 );
var same = new Something( 1, 2, 3 );
这种构造函数方法在使用或不使用new关键字时都有效:
function Something(foo, bar){
if (!(this instanceof Something)){
var obj = Object.create(Something.prototype);
return Something.apply(obj, arguments);
}
this.foo = foo;
this.bar = bar;
return this;
}
它假设支持Object。创建,但如果你支持旧的浏览器,你总是可以填充它。请参阅MDN上的支持表。
下面是一个JSBin,可以看到它在控制台输出中的作用。