您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JavaScript将.apply()与'new'运算符配合使用。这可能吗?

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的上下文。但是,出于语法原因,它是必需的。现在,进行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 2022/1/1 18:15:46 有483人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶