前言
我是歌谣 我有个兄弟 巅峰的时候排名c站总榜19 叫前端小歌谣 曾经我花了三年的时间创作了他 现在我要用五年的时间超越他 今天又是接近兄弟的一天人生难免坎坷 大不了从头再来 歌谣的意志是永恒的 放弃很容易 但是坚持一定很酷
导语
apply使用
代码部分
Function.prototype.myApply = function (context, args) { //这里默认不传就是给window,也可以用es6给参数设置默认参数 context = context || window args = args ? args : [] //给context新增一个独一无二的属性以免覆盖原有属性 const key = Symbol() context[key] = this //通过隐式绑定的方式调用函数 const result = context[key](...args) //删除添加的属性 delete context[key] //返回函数调用的返回值 return result } var name = 'geyao' var fangfang = { name: 'fangfang', fang: function () { console.log(this.name) }, fun: function () { console.log(this,"this") setTimeout( function () { this.fang() }.myApply(this), 100 ) }, } fangfang.fun() //fangfang