在 ECMAScript 2015 中调用 Reflect.apply() 而不是 Function.prototype
Is there any benefit to call Reflect.apply() over Function.prototype.apply() in ECMAScript 2015?
我只是想知道是否有任何充分的理由打电话:
Reflect.apply(myFunction, myObject, args);
而不是:
myFunction.apply(myObject, args);
您可以在规范中比较Function.prototype.apply
和Reflect.apply
的定义。
基本上它们是等价的,但有区别:如果参数列表是null
或undefined
,Function.prototype.apply
将调用没有参数的函数,Reflect.apply
将抛出。
function func() {
return arguments.length;
}
func.apply(void 0, null); // 0
Reflect.apply(func, void 0, null); // TypeError: null is not a non-null object
另一个区别是,当你使用func.apply
时,你假设
-
func
是一个Function
实例,即它继承自Function.prototype
-
func
没有apply
自己的财产,会给Function.prototype.apply
阴影
但Reflect.apply
并不需要这样做。例如
var obj = document.createElement('object');
typeof obj; // "function" -- can be called
obj.apply; // undefined -- does not inherit from Function.prototype
Reflect.apply(obj, thisArg, argList); // -- works properly
var func = a => a;
func.apply = a => 0;
func.apply(void 0, [123]); // 0 -- Function.prototype.apply is shadowed by an own property
Reflect.apply(func, void 0, [123]); // 123 -- works properly
另请参阅SO问题"反射对象在JavaScript中做什么?",其中包括以下文本:
现在我们有了模块,对于之前在 Object 上定义的许多反射方法来说,"@reflect"模块是一个更自然的地方。出于向后兼容性的目的,Object 上的静态方法不太可能消失。但是,新方法可能应该添加到"@reflect"模块而不是对象构造函数中
我的理解是,在以前的JS迭代中,与"反射"相关的工具分散在语言中,作为对象原型和函数原型的一部分。Reflect
目标是努力将它们集中在一个屋檐下。
因此,就您的问题而言,尽管存在差异(参见Oriol的答案(,但两者存在的原因是ES规范中普遍转向面向未来的反射工具。
我能想到的一种用途是在流管理或执行函数数组的函数中使用 Reflect.apply
function execFuncs(funcArr){
var obj = this.someObj;
funcArr.forEach(function(func){
Reflect.apply(func,obj)
});
}
哪个更方便
function execFuncs(funcArray){
var obj = this.someObj;
funcArray.forEach(function(func){
func.prototype.apply(obj)
})
}
因为你有更多的控制权。
相关文章:
- 直接在函数声明上使用function.prototype.bind
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- Function.prototype.call和Function.protoype.all只应用一个参数
- requestAnimationFrame的Function.prototype.bind导致属性不可读
- 对象如何获取 Function.prototype 方法
- Function.prototype上调用方法的性质
- 为什么在Function.prototype上没有原型属性
- Function.prototype.propertyname === Object.propertyname is t
- Function.prototype.apply:参数列表的类型错误
- node.js 使用 function.prototype.apply 执行的泛型函数
- Snippet usefulness - Function.prototype.call.bind(Function.p
- 使用 Function.prototype.bind.apply(Obj, args) 将参数传递给对象,只传递第一个元
- Function.prototype.call.bind 是如何工作的
- 在 Function.prototype.method 中返回它有什么作用
- JavaScript 中 Function.prototype 和 Object.prototype 之间的区别
- 使用 Function.prototype.bind 或保存的引用
- Phantomjs Function.prototype.bind
- Function.prototype.bind with null 作为参数
- Number.prototype.function无法处理jQuery(.).val()的结果
- function()存在,但prototype function()不存在;不存在.为什么