题解 | #_call函数#

_call函数

http://www.nowcoder.com/practice/22df1ed71b204a46b00587fdb780b3ab

Function.prototype._call = function(context,..args){
	// 处理容错
    context = (typeof context === 'object' ? context : window)
    args = args ? args : []
    //给context新增一个独一无二的属性以免覆盖原有属性
    const key = Symbol();
    context[key] = this;
    //通过隐式绑定的方式调用函数
    const result = context[key](...args);
    //删除添加的属性
    delete context[key]
    //返回函数调用的返回值
    return result
}
全部评论
参数里面的args,应该前面三个点吧
1 回复 分享
发布于 2022-07-04 23:31
call 是 ES6 以前的,传入的参数可以用 arguments 替换 ES6 的参数解构
1 回复 分享
发布于 2022-05-01 00:54

相关推荐

2025-12-17 17:15
华东师范大学 运营
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务