JavaScript將.apply()與'new'運(yùn)算符配合使用。這可能嗎?
使用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]));
即使基于特殊的構(gòu)造函數(shù),此方法和基于eval的解決方案仍然是唯一可以正常工作的解決方案Date:
var date = newCall(Date, 2012, 1);console.log(date instanceof Date); // true
一點(diǎn)解釋:我們需要new在一個(gè)帶有有限數(shù)量參數(shù)的函數(shù)上運(yùn)行。該bind方法允許我們這樣做:
var f = Cls.bind(anything, arg1, arg2, ...);result = new f();
該anything參數(shù)無關(guān)緊要,因?yàn)閚ew關(guān)鍵字resetf的上下文。但是,出于語法原因,它是必需的。現(xiàn)在,進(jìn)行bind調(diào)用:我們需要傳遞可變數(shù)量的參數(shù),所以就可以了:
var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]);result = new f();
讓我們將其包裝在一個(gè)函數(shù)中。Cls被作為arugment 0傳遞,它將成為我們的anything。
function newCall(Cls /*, arg1, arg2, ... */) { var f = Cls.bind.apply(Cls, arguments); return new f();}
實(shí)際上,f根本不需要臨時(shí)變量:
function newCall(Cls /*, arg1, arg2, ... */) { return new (Cls.bind.apply(Cls, arguments))();}
最后,我們應(yīng)該確保這bind確實(shí)是我們所需要的。(Cls.bind可能已被覆蓋)。因此,將其替換為Function.prototype.bind,我們得到的最終結(jié)果如上所述。
解決方法在JavaScript中,我想創(chuàng)建一個(gè)對(duì)象實(shí)例(通過new運(yùn)算符),但是將任意數(shù)量的參數(shù)傳遞給構(gòu)造函數(shù)。這可能嗎?
我想做的是這樣的(但是下面的代碼不起作用):
function Something(){ // init stuff}function createSomething(){ return new Something.apply(null,arguments);}var s = createSomething(a,b,c); // ’s’ is an instance of Something
答案
從這里的響應(yīng)中可以明顯看出,沒有內(nèi)置的方法可以.apply()與new接線員通話。但是,人們提出了一些非常有趣的解決方案。
我更喜歡的解決方案是MatthewCrumley提出的解決方案(我已對(duì)其進(jìn)行了修改以通過該arguments屬性):
var createSomething = (function() { function F(args) {return Something.apply(this,args); } F.prototype = Something.prototype; return function() {return new F(arguments); }})();
