/**
* @description 模拟实现 call
* 1. 改变 this 指向
* 2. 执行函数
*/
// 用法
var foo = {
value: 1
}
function bar() {
console.log(this.foo)
}
bar.call(foo) // 把 bar 的 this 绑定到 foo 上, 只要将bar函数变成foo的属性即可使用foo的this,用完删除
// 变成这样就 OK 啦
var foo = {
value: 1,
bar: function() {
console.log(this.value)
}
}
/**
* 模拟实现第一步
* 1. foo.fn = bar
* 2. foo.fn()
* 3. delete foo.fn()
*/
Function.prototype.mycall = function(context) {
context.fn = this
context.fn()
delete context.fn
}
/**
* 模拟实现第二步
* call 函数可以传递参数并执行, 参数不确定
* var foo = {
value: 1
}
function bar (name, age) {
console.log(name, age, this.value)
}
bar.call(foo, 'agren', 26)
*/
Function.prototype.mycall = function(context) {
var context = context || window
context.fn = this
var args = [...arguments].slice(1) // 拿到第一个到最后一个的参数
var result = context.fn(...args)
delete context.fn
return result
}
/**
* @description 模拟实现 apply 与 call 类似,只不过,只有两个参数,第二个参数是数组
*/
Function.prototype.myApply = function(context = window) {
context.fn = this
var result
if (arguments[1]) {
result = context.fn(...arguments[1]) // 还是只取第一个到最后一个的参数
} else {
result = context.fn()
}
delete context.fn
return result
}
/**
* @description 模拟实现 bind 方法 与上面两个类似
* 1. 设置this 为执行的值,并会返回函数
* 2. 调用函数时,将给定参数列表,作为原函数参数列表的千若干项
*/
// bind 第一步
Function.prototype.myBind = function(...rest1) {
const self = this
const context = rest1.shift() // 拿到第一个参数
return function(...rest2) {
return self.apply(context, [...rest1, ...rest2])
}
}
模拟实现 call apply bind
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- js的call, apply, bind函数的实现,这是老生常谈了,这些都是改变函数执行上下文的函数;今天我也来实...
- Function.prototype.llqCall = function(o){if(typeof this !...
- 本文首发我的个人博客:前端小密圈,评论交流送1024邀请码,嘿嘿嘿😄。 来自朋友去某信用卡管家的做的一道面试题,用...