当前位置: 首页 > news >正文

前端:JavaScript中的this

前端:JavaScript中的this

    • 1. this的指向
    • 2. 指定this的值
    • 3. 手写call方法
    • 4. 手写apply方法
    • 5. 手写bind方法

1. this的指向

在非严格模式下,总是指向一个对象;在严格模式下可以是任意值。
开启严格模式,如果是为整个脚本开启,直接在当前脚本第一行代码写上如下代码:

'use strict'

如果是为函数开启严格模式,则是在函数第一行写上上述代码:

function a(){'use strict'
}
  1. 全局执行环境中,指向全局对象即window(严格、非严格模式);
  2. 函数内部,取决于函数被调用的方式。如果直接调用this,严格模式下为undefined,非严格模式下为全局对象(window);如果采用对象方法调用的this, 则指向调用者(严格、非严格)

直接调用

function a(){console.log(this);}function b() {'use strict'console.log(this);}a() // windowb() // undefined

对象方法调用

const food = {name:'西瓜',dec(){console.log(this)}
}
const food2 = {name: '香蕉',dec() {'use strict'console.log(this)}
}
food.dec() // food这个对象
food2.dec() // food2这个对象

2. 指定this的值

  • 调用时指定,call、apply(数组传递参数)
  • 创建时指定,bind,箭头函数

call使用如下:

func.call(thisArg,参数1,参数2...)
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}func.call(person,1,2)

在这里插入图片描述

apply使用如下:

func.apply(thisArg,[参数1,参数2...])
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}func.apply(person,[2,3])

在这里插入图片描述

bind使用如下:

func.bind(thisArg,绑定参数1,绑定参数2...)
function func(num1,num2){console.log(this);console.log(num1,num2);}const person = {name:'baidu'}const bindFunc = func.bind(person,77)bindFunc(88)

在这里插入图片描述

箭头函数
使用普通函数,此时最里层的this指向window

const person = {name:'baidu',hh(){console.log(this);setTimeout(function(){console.log(this);},1000)}}person.hh()

在这里插入图片描述
使用箭头函数,最里层的this指向其外层的this值。

const person = {name:'baidu',hh(){console.log(this);setTimeout(()=>{console.log(this);},1000)}}person.hh()

在这里插入图片描述

3. 手写call方法

任何定义的函数都是属于Function类型,那么只需要在Function其原型上添加一个call方法,其他自定义的函数上均可以使用call方法,这里定义的call方法名为myCall。

Function.prototype.myCall = function(thisArg,...args){thisArg.f = this// this指向调用myCall的那个函数const res = thisArg.f(...args)// 传递进来的args参数为数组类型delete thisArg.freturn res
}

但是上述还存在一个问题,那就是如果thisArgs这个对象上也刚好存在f属性,上述操作会把原对象thisArgs的f属性去掉。因此可以考虑使用Symbol,Symbol无论调用多少次,其返回值均是唯一的。

function func(num1, num2) {console.log(this);console.log(num1, num2);}const person = {name: 'baidu'}Function.prototype.myCall = function(thisArg,...args){const key = Symbol('key');thisArg[key] = this// this指向调用myCall的那个函数const res = thisArg[key](...args)delete thisArg[key]return res}func.myCall(person,22,33)

运行结果:
在这里插入图片描述

4. 手写apply方法

和myCall类似,只是传递参数不同而已。

function func(num1, num2) {console.log(this);console.log(num1, num2);
}
const person = {name: 'baidu'
}
Function.prototype.myApply = function(thisArgs,args){const key = Symbol('key')thisArgs[key] = thisconst res = thisArgs[key](...args)delete thisArgs[key]return res
}
func.myApply(person,[44,66])

在这里插入图片描述

5. 手写bind方法

function func(num1, num2) {console.log(this);console.log(num1, num2);
}
const person = {name: 'baidu'
}
Function.prototype.myBind = function(thisArgs,...args){return (...newArgs)=>{return this.call(thisArgs,...args,...newArgs)}
}
const myBindFunc = func.myBind(person,1)
myBindFunc(2)

箭头函数中this指向func,即其调用者(外层this的指向)
在这里插入图片描述

http://www.lryc.cn/news/434429.html

相关文章:

  • Zynq7020 SDK 初学篇(5)- 中断
  • 如何清缓存
  • 《计算机算法设计与分析》笔记
  • 智能指针怎么就智能了?
  • mysql 限制用户登录次数超过3次就 锁定账户在一段时间内不运行操作
  • 深度学习中的常用线性代数知识汇总——第二篇:行列式、逆矩阵、特征值与特征向量
  • 《MaPLe: Multi-modal Prompt Learning》中文校对版
  • MFC修改控件ID的详细说明
  • MySQL高可用配置及故障切换
  • AI模型一体机:智能办公的未来
  • jina的Embedding Reranker
  • Prompt Engineer: 使用Thought来提升LLM的回复能力
  • tekton构建标准ci(clone repo, test, build push img)
  • 【电力系统】复杂网络分析在电力系统规范中的应用
  • CDGA|推动数据治理与传统产业深度融合:策略与实践路径
  • 【FastAPI】离线使用Swagger UI 或 国内网络如何快速加载Swagger UI
  • Linux:从入门到放弃
  • SVM 监督学习
  • 奖励模型的训练
  • Ubuntu22.04之禁止内核自动更新(二百六十八)
  • kaggle题-房价预测(Pytorch),手把手教,全文代码解释
  • PulseSensor心率传感器详解(STM32)
  • NISP 一级 | 3.1 网络基础知识
  • 模拟网络丢包常用方法以及工具
  • ABC 370 E - Avoid K Partition
  • C++: set与map容器的介绍与使用
  • 单片机-STM32 看门狗(八)
  • iOS 18.1将上线新功能,可惜这波国内的小伙伴无缘了
  • MySQL中DML操作(二)
  • LLMs技术 | 整合Ollama实现本地LLMs调用