亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【九月打卡】第8天 手寫 bind call apply

標簽:
JavaScript

课程名称2周刷完100道前端优质面试真题
课程章节:第8章 前端面试技能拼图6: 编写高质量代码 - 正确,完整,清晰,鲁棒
主讲老师双越
课程内容
今天学习的内容包括:
8-14 手写函数bind功能
8-15 【连环问】手写函数call和apply功能
今天主要就是讲了 call apply bind 的写法。重点就是分辨清楚三种方法怎么用,外加如何实现绑定 this。
课程收获

手写 bind

注意点:

  • bind返回函数,
  • 返回函数再次传参,需要参数拼接。
Function.prototype.cusBind = function (context, ...bindArg) {
    // 当前函数
	const self = this;
	return function(...arg) {
		const newArg = bindArg.concat(arg);
		return self.apply(context, newArg)
	}
}
function fn(a, b, c) {
  console.info(this, a, b, c);
}
let bindFn = fn.cusBind({x: 100}, 10, 20); 
bindFn(30)  // {x: 100} 10 20 30

手写 call

注意点:

  • 传入 this 为空 ( globalThis ) 和 非对象时处理 (new Object(context))。
  • 函数绑定 this 时设置的,为了避免重复覆盖掉 this 原有属性,要用Symbol。且最后记得删除对应属性。
Function.prototype.cusCall = function (context, ...callArg) {
    if (context == null) context = globalThis;
    if (typeof context !== 'object') {
	    context = new Object(context);
    }
	// fn 通过作为属性绑定 this
    const fnKey = Symbol();
    context[fnKey] = this;
    const res = context[fnKey](...callArg);
   // this 删除 key
    delete context[fnKey];
    return res
}
function fn(a, b, c) {
  console.info(this, a, b, c);
}
fn.cusCall({x: 100}, 10, 20, 30); // {x: 100, Symbol(): ƒ} 10 20 30

手写 apply

这个和 call 基本一模一样,只是入参有区别,第二个参数传参是数组。

Function.prototype.cusApply = function (context, applyArr) {
    if (context == null) context = globalThis;
    if (typeof context !== 'object') context = new Object(context);
	// fn 通过作为属性绑定 this
    const fnKey = Symbol();
    context[fnKey] = this;
    const res = context[fnKey](...applyArr);
   // this 删除 key
    delete context[fnKey];
    return res
}
function fn(a, b, c) {
  console.info(this, a, b, c);
}
fn.cusApply({x: 100}, [10, 20, 30]); // {x: 100, Symbol(): ƒ} 10 20 30

再写 bind

显然手写 bind 是没必要借助 apply 的。虽然老师没讲,自己写一下。

Function.prototype.cusBind1 = function (context, ...bindArg) {
	if (context == null) context = globalThis;
    if (typeof context !== 'object') context = new Object(context);
    const fnKey = Symbol();
    context[fnKey] = this;
	return function(...arg) {
		return context[fnKey]([...bindArg, ...arg]);
	}
}
function fn(a, b, c) {
  console.info(this, a, b, c);
}
let bindFn = fn.cusBind1({x: 100}, 10, 20); 
bindFn(30)  // {x: 100} 10 20 30

结束!

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消