为了掌握函数式编程的窍门,我重写了许多 JavaScript 的高阶函数,但我一直停留在 apply
上。是否可以用 JavaScript 编写 apply
?假设所有其他 native 函数都存在,并且正在使用 ES5 规范。
最佳答案
对于 ES5 及更低版本,我认为如果不使用 eval
就无法做到这一点(见下文)。您几乎可以通过args.length
上的massive switch语句来做到这一点,但在某些时候,您只需要说有一个限制该开关
中的案例
数量。
Function.prototype.newApply = function(thisArg, args) {
switch (args.length) {
case 0: return this.call(thisArg);
case 1: return this.call(thisArg, args[0]);
case 2: return this.call(thisArg, args[0], args[1]);
// etc..
default: throw new Error("She canna tek any more!");
}
};
如果您允许eval
,那么您绝对可以做到 - 完全归功于 blex建议评估
:
Function.prototype.newApply = function(thisArg, args) {
var f = this,
call = "f.call(thisArg",
i;
for (i = 1; i < args.length; ++i) {
call += ", args[" + i + "]";
}
call += ")";
return eval(call);
};
实例:
Function.prototype.newApply = function(thisArg, args) {
var f = this,
call = "f.call(thisArg",
i;
for (i = 0; i < args.length; ++i) {
call += ", args[" + i + "]";
}
call += ")";
return eval(call);
};
var obj1 = {
foo: "foo",
bar: "bar"
};
var obj2 = {
foo: "F",
bar: "B"
};
function caps(o1, o2) {
var k;
snippet.log("this.x = " + this.x);
for (k in o1) {
o1[k] = o1[k].toUpperCase();
}
for (k in o2) {
o2[k] = o2[k].toLowerCase();
}
}
caps.newApply({x:42}, [obj1, obj2]);
snippet.log(JSON.stringify(obj1));
snippet.log(JSON.stringify(obj2));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
或者如果您想使用Array#reduce
:
Function.prototype.newApply = function(thisArg, args) {
var f = this,
call = args.reduce(function(acc, _, index) {
return acc + ", args[" + index + "]";
}, "f.call(thisArg") + ")";
return eval(call);
};
<小时/>
您在问题中提到了 ES5,但只是为了完整起见:由于扩展运算符 (...
),在 ES6 中这真的很容易:
Function.prototype.newApply = function(thisArg, args) {
return this.call(thisArg, ...args);
};
关于javascript - 是否可以重写JavaScript的apply函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051873/