1 Star 1 Fork 0

废柴阿蔚/JavaScriptNote

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.js 791 Bytes
一键复制 编辑 原始数据 按行查看 历史
废柴阿蔚 提交于 2020-04-09 22:39 +08:00 . 测试多个仓库
Function.prototype.myCall = function (context) {
/* 1 校验调用call的是否是函数
2 设置默认上下文为全局上下文 */
if (typeof this !== 'function') {
throw new TypeError('is not a function!')
}
context = context || window || global
// step1: 把函数挂到目标对象上(这里的 this 就是我们要改造的的那个函数)
context.func = this
const args = [...arguments].slice(1)
// step2: 执行函数
const result = context.func(...args)
// step3: 删除 step1 中挂到目标对象上的函数,把目标对象”完璧归赵”
delete context.func
return result
}
var me = {
name: 'zhangsan'
}
function showName(doing) {
console.log(`${this.name} is ${doing}`)
}
showName.myCall(me,'eating') // zhangsan is eating
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/studyLi/JavaScriptNote.git
git@gitee.com:studyLi/JavaScriptNote.git
studyLi
JavaScriptNote
JavaScriptNote
master

搜索帮助