建议看这片文章时能够点击音乐🎵,来个单曲循环,美滋滋javascript
做用:call和apply都是替换函数内错误的this
var a = { value:1 } var b = function(){ console.log(this.value) // 若是不对this进行绑定执行bar() 会返回undefined } b.call(a) //1
去除繁琐的讲解,一步到位本身模拟call的用法写一个函数,达到相同目的git
Function.prototype.myCall = function(context){ var context = context || window; //当没传入值时候,就是指全局window context.fn = this; //把调用myCall前的方法缓存下来 var args = [...arguments].slice(1);//使用...打散传入值,并去除第一方法,获得一个数组 var result = context.fn(...args);//把数组打散,把dinging 18传入b方法中 delete context.fn; //删除 return result } var a = { value:1 } var b = function(name,age){ console.log(this.value) console.log(name) console.log(age) } b.myCall(a,"dingding",18)
apply的方法和 call 方法的实现相似,只不过是若是有参数,以数组形式进行传递
apply这个API平时使用的场景,代码以下:github
var a = { value:1 } var b = function(name,age){ console.log(this.value) console.log(name) console.log(age) } b.apply(a,["dingding",18])
直接上模拟apply功能代码数组
Function.prototype.myApply = function(context){ var context = context || window; context.fn = this; var result; if(arguments[1]){ result = context.fn(...arguments[1]) }else{ result = context.fn() } delete context.fn return result } var a = { value:1 } var b = function(name,age){ console.log(this.value) console.log(name) console.log(age) } b.myApply(a,["dingding",18])
参考资料缓存