您的位置:首頁技術文章
文章詳情頁

JavaScript將.apply()與'new'運算符配合使用這可能嗎?

瀏覽:92日期:2024-05-09 13:28:18
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解決JavaScript將.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的上下文。但是,出于語法原因,它是必需的?,F在,進行bind調用:我們需要傳遞可變數量的參數,所以就可以了:

var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]);result = new f();

讓我們將其包裝在一個函數中。Cls被作為arugment 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接線員通話。但是,人們提出了一些非常有趣的解決方案。

我更喜歡的解決方案是MatthewCrumley提出的解決方案(我已對其進行了修改以通過該arguments屬性):

var createSomething = (function() { function F(args) {return Something.apply(this,args); } F.prototype = Something.prototype; return function() {return new F(arguments); }})();

標簽: JavaScript
国产综合久久一区二区三区