網(wǎng)站空間有哪些html期末大作業(yè)個(gè)人網(wǎng)站制作
1. 函數(shù)調(diào)用時(shí)指定
call -- 接收一個(gè)參數(shù)列表
apply -- 接收一個(gè)參數(shù)數(shù)組
2. 創(chuàng)建時(shí)指定this的值
bind -- 返回一個(gè)函數(shù)? 傳參方式與call相同
箭頭函數(shù) -- 其this值取決于上級(jí)作用域中的this值
<script>// 如何指定this的值// 1. 調(diào)用時(shí)指定this// 2. 創(chuàng)建時(shí)指定this// 1. 調(diào)用時(shí)指定thisfunction fun(num1, num2) {console.log(this)console.log(num1, num2)}const person = {name: 'hjy'}// 1.1 callfun.call(person, 1, 2)// 1.2 applyfun.apply(person, [3, 4])// 2.創(chuàng)建時(shí)指定this// 2.1 bind 返回一個(gè)函數(shù)const bindFun = fun.bind(person, 5) // 此處可傳遞多個(gè)參數(shù) 不一定是一個(gè)bindFun(6)// 2.2 箭頭函數(shù) const food = {name: '新疆炒米粉',eat() {console.log(this) //為food 取決于調(diào)用的對(duì)象 setTimeout(() => {console.log(this) // 為food 取決于外部作用域的this值}, 1000)setTimeout(function () {console.log(this) // 為全局對(duì)象(window)}, 1000)}}food.eat()</script>