- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
一段时间以来,我一直在使用以下函数来创建未知类的实例:
Kernel.prototype._construct = function (constr, args) {
function F() {
constr.apply(this, args); // EXCEPTION!
}
F.prototype = constr.prototype;
return new F();
};
如果我使用原型(prototype),一切正常:
function Person(name, surname) {
this.name = name;
this.surname = surname;
}
var person = Kernel._construct(Person, ["name", "surname"]); // WORKS!
但是,有些人在 Node v4+ 中使用 ES6 原生类来使用我的库:
class Person {
constructor(name, surname) {
this.name = name;
this.surname = surname;
}
}
var person = Kernel._construct(Person, ["name", surname]); // EXCEPTION!
他们收到一个错误:
TypeError: Class constructors cannot be invoked without 'new'
我需要能够使用未知数量的参数调用构造函数。关于如何解决此问题的任何想法?
最佳答案
有多种方法可以做到这一点:
使用 Function
对象的方法:
Kernel.prototype._construct = function (constr, args) {
return new (Function.prototype.bind.apply(constr, [null].concat(args)));
};
我们在这里 applying args
作为 bind
的参数.目标是拥有一个无需参数即可调用的函数,以便我们可以调用 new x()
。 bind
为我们做了这个,但我们需要正确设置它。语法是:
func.bind(thisArg[, arg1][, args2...])
// calling the results from the above is like
// thisArg.func(arg1, arg2...);
我们想使用 constr
作为绑定(bind)函数,args
中的项目作为参数。我们不关心 thisArg
。为此,我们需要将 args
数组“转换”为参数。 apply
调用执行此操作:
func.apply(thisArg[, args]);
// calling the results from the above is like
// thisArg.func(args[0], args[1]...);
apply
其实就是在调用bind
。第一项 [null]
很重要,因为我们要调用 bind
,其中 thisArg
为 null
- 就像这个:constr.bind(null, args[0], args[1]...)
。
使用 ES2015 Spread operator :
Kernel.prototype._construct = function (constr, args) {
return new constr(...args);
};
这样就简单多了,但是有两个问题:
eval()
/new Function(...)
) - 不建议这样做。使用 Reflect
内置对象。
Kernel.prototype._construct = function (constr, args) {
return Reflect.construct(constr, args);
};
这也很简单,但在支持方面更落后(基本上你必须使用 babel)。
关于javascript - ES6 类中的 constr.apply(this, args),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193310/
我被这种奇怪的事情难住了。 假设我有这个数组: var array = [{ something: 'special' }, 'and', 'a', 'bunch', 'of', 'paramet
假设我们有这样的代码: let fn1 = Function.apply.bind(Math.max, null); fn1([1, 10, 5]); // returns 10 我知道它是 ES6
所以我尝试通过数据绑定(bind)调用我的 viewModel 原型(prototype)上的方法。我通过“单击”将两个不同的元素数据绑定(bind)到同一方法。当我单击第一个按钮(“新游戏”按钮)时
观察以下代码 trait Example { type O def apply(o: O) def f(o: O) = this.apply(o) } 在Scala中编译良好。我希望我可以
我知道 apply f in H 可用于将假设应用于函数,并且我知道 apply f with a b c 可用于提供参数直接应用 f 时,它无法自行推断。 是否可以以某种方式将两者结合使用? 最佳答
这个问题已经有答案了: How to override apply in a case class companion (10 个回答) 已关闭 6 年前。 我正在尝试重载案例类的 apply 方法:
我有一个自定义的Grails 4.x配置文件。我想为我的应用程序生成一个“apply from”条目。 apply from:"${rootProject.projectDir}/gradle/clo
传统上对象继承如下所示: function Parent() { console.log('parent constructor'); } Parent.prototype.method = f
今天在检查Jasmine 的源代码时here我偶然发现了以下内容: if (queueableFn.timeout) { timeoutId = Function.prototype.appl
据我所知,关键字new会使用this创建一个包含函数中定义的属性的对象。但我不知道如何应用 使用 apply 将其他函数链接到该函数。并且创建的对象在这些函数中具有属性。有人能弄清楚代码中发生了什么吗
我一直在我的 InitComponent 中使用 Ext.Apply,就像这样 Ext.apply(that, { xtype: 'form', items: [.
我们有数百个存储库,并定期从上游接收补丁。作业应用这些补丁 git apply --check .如果没有错误,则应用补丁 git apply 并且更改已提交。如果有任何错误,补丁将标记为 conf
我最近通过调用 console.log.toString() 查看了 firebugs console.log 的代码并得到了这个: function () { return Function.app
拿这个代码: $scope.$apply(function(){ $scope.foo = 'test'; }); 对比这个: $scope.foo = 'test'; $scope.$app
我在 Oracle-12c 中有一个类似于典型论坛的架构 accounts , posts , comments .我正在编写一个查询来获取... 一位用户 该用户的所有帖子 对每个帖子的评论 以及每
我试图更好地理解在 Angular 中使用 $timeout 服务作为一种“安全 $apply”方法的细微差别。基本上在一段代码可以运行以响应 Angular 事件或非 Angular 事件(例如 j
到目前为止,我使用的是 this当我有多个时间序列要预测时,我使用了 Hyndman 教授的方法。但是当我有大量的 ts 时它相当慢。 现在我正在尝试使用 apply() 函数,如下所示 librar
我听说过很多关于 pandas apply 很慢的说法,应该尽可能少用。 我这里有个情况: df = pd.DataFrame({'Date': ['2019-01-02', '2019-01-03'
在学习Javascript时,我尝试重新声明函数的apply属性。到目前为止没有问题。 function foo() { return 1; } alert(foo()); // 1 alert(fo
所以我正在做 learnRx http://reactive-extensions.github.io/learnrx/我有一个关于制作 mergeAll() 函数的问题(问题 10)。 这是我的答案
我是一名优秀的程序员,十分优秀!