如何在不使用apply的情况下实现Function.prototype.apply
How to implement Function.prototype.apply without using apply?
function sumArguments () {
var value = this instanceof Number ? this : 0;
for (var i = 0; i < arguments.length; i++) {
value += arguments[i];
}
return value;
}
Function.prototype.apply1 = function() {
var fn = this;
var args = Array.prototype.slice.call(arguments);
var context = args.shift();
var newArgs = args[0];
var boundfn = fn.bind(context, newArgs);
return boundfn();
}
console.log(sumArguments.apply1(4, [1, 2, 3])); --> should equal 10
如何在不使用应用程序的情况下实现应用程序?我陷入了如何转换要传入的 [1,2,3] 数组作为 1,2,3 进行绑定的困扰。
我能想到的唯一方法是使用eval
.
function apply(fn, args){
var stringArgs = args.reduce(function(acc, el, i){
var argString = 'args[' + i + ']';
return acc += i === 0 ? argString : ',' + argString;
});
return eval('fn(' + stringArgs + ')');
}
function test(first, second, third){
console.log(first, second, third);
}
apply(test, [1, 2, 3]);
虽然,我不明白你为什么需要这个。这也不适用于许多数据类型,如对象或字符串。不过,它确实适用于数字。
编辑:现在它适用于任何数据类型。
您在这里的目标是使用上下文和提供的参数调用fn.bind
,对吗?
如果您使用的是 ES6,则需要点差运算符...
:
foo(...[1,3,5]); //equivalent to foo(1, 3, 5)
fn.bind(context, ...args);
否则,您可以在bind
上拨打apply
:
fn.bind.apply(fn, [context].concat(args));
将fn
作为fn.bind
的上下文传递将保留其正常行为,并且您需要将要传递的所有参数组合到单个数组中,因此进行串联。
不知道为什么需要使用apply,但这里有sum函数可以给你你想要的东西。 它应该能够处理许多内部数组并忽略对象。
function sumArguments() {
var value = this instanceof Number ? this : 0;
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'number') {
value += arguments[i];
} else if (typeof arguments[i] === 'object' &&
typeof arguments[i].length !== 'undefined') {
for (var j = 0; j < arguments[i].length; j++) {
if (typeof arguments[i][j] === 'number') {
value += arguments[i][j];
} else if (typeof arguments[i][j] === 'object' &&
typeof arguments[i][j].length !== 'undefined') {
value += sumArguments(arguments[i][j]);
}
}
}
}
return value;
}
console.log(sumArguments(4, [1, 2, 3, [7, 8]], 5, 'a', 2));
相关文章:
- 如何在未直接触发的情况下停止事件
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- 在不阻止默认行为的情况下检测IE10中的缩放
- 如何在不传递此信息的情况下查找被调用的元素
- 如何在不刷新页面的情况下更新显示框
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 在我的情况下,如何进行http请求
- 在不知道深度或父属性的情况下从对象中删除属性
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- 如何在不影响其他元素的情况下扩展DIV
- 是否可以在不更改上下文的情况下调用函数.apply
- 使用 apply() 与 call(),在这种情况下使用哪一个
- 如何在不使用apply的情况下实现Function.prototype.apply
- 为什么在这种特殊情况下视图没有更新?($scope.$apply)
- 为什么apply在这种情况下只接受一个参数
- 为什么要使用'apply'在这种情况下的函数