【備戰春招】第四天 前端學習筆記
標簽:
JavaScript
课程信息
课程名称:一天时间高效准备前端技术一面 匹配大厂面试要求
章节名称:第6章 作用域和闭包
讲师:双越
课程描述
介绍作用域,自由变量,闭包,this等。
收获
this
//作为普通函数
function fn1() {
console.log(this)
}
fn1() //window
//使用call apply bind
fn1.call({ x: 100 }) //{x: 100}
const fn2 =fn1.bind({x: 200})
fn2() //{ x: 200 } bind会返回一个新函数,需要重新执行这个函数
//作为对象方法 + 箭头函数
const zhangsan = {
name: ‘张三’,
sayHi() {
console.log(this) //this即当前对象
},
wait() {
setTimeout(function() {
console.log(this) //this === window
})
},
waitAgain() {
setTimout(() => {
console.log(this) //this即当前对象
})
}
}
面试题
手写 bind
Function.prototype.bind1 = function () {
// 将参数拆解为数组
const args = Array.prototype.slice.call(arguments)
const t = args.shift()
const self = this
return function () {
return self.apply(t, args)
}
}
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦