最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript this的多種場(chǎng)景使用

 更新時(shí)間:2025年09月05日 08:55:28   作者:啦工作呢  
this是JavaScript語言的一個(gè)關(guān)鍵字,代表函數(shù)運(yùn)行時(shí)自動(dòng)生成的內(nèi)部對(duì)象,僅能在函數(shù)內(nèi)部使用, 下面就來介紹一下JavaScript this的多種場(chǎng)景使用,感興趣的可以了解一下

什么是 this

this 是 JavaScript 語言的一個(gè)關(guān)鍵字,代表函數(shù)運(yùn)行時(shí)自動(dòng)生成的內(nèi)部對(duì)象,僅能在函數(shù)內(nèi)部使用。其值會(huì)隨函數(shù)使用場(chǎng)合不同而變化,但核心原則是:this 指向調(diào)用函數(shù)的那個(gè)對(duì)象

this 的多種指向場(chǎng)景

1. 全局范圍內(nèi)的 this

在全局作用域中,this 指向全局對(duì)象(瀏覽器環(huán)境中為 window):

var name = 'name1';
console.log(name);               // "name1"
this.name = 'name2';
console.log(name);               // "name2"
console.log(this.name);          // "name2"
window.name = 'name3';
console.log(name);               // "name3"
console.log(this.name);          // "name3"
console.log(window.name);        // "name3"

2. 純粹的函數(shù)調(diào)用

函數(shù)在全局范圍內(nèi)調(diào)用時(shí),this 指向全局對(duì)象:

// 案例1
function test() {
  this.x = 1;
  alert(this.x);  // 1
}
test();
 
// 案例2
var x = 1;
function test() {
  alert(this.x);  // 1
}
test();
 
// 案例3
var x = 1;
function test() {
  this.x = 0;
}
test();
alert(x);  // 0

3. 事件處理中的 this

在事件處理函數(shù)中,this 指向觸發(fā)事件的元素:

var btn = document.getElementById("btn");
btn.onclick = function() {
  this.value = "按鈕";  // this 指向 btn 元素
}

4. HTML 結(jié)構(gòu)中的 this

在 HTML 標(biāo)簽的事件屬性中,this 指向綁定事件的元素本身:

<button onclick="fun(this)">點(diǎn)擊</button>
<script>
function fun(_this) {
  console.log(_this);  // 輸出當(dāng)前點(diǎn)擊的 button 元素
}
</script>

5. 對(duì)象方法中的 this

對(duì)象字面量中的方法,this 指向該對(duì)象本身:

var obj = {
  name: 'jack',
  say: function() {
    console.log('obj中的this', this.name);  // this 指向 obj
  }
}
obj.say();  // "obj中的this jack"

6. 定時(shí)器中的 this

定時(shí)器(setTimeout/setInterval)中的回調(diào)函數(shù),this 默認(rèn)指向 window:

// 問題示例
box1.onclick = function() {
  var num = 0;
  setInterval(function() {
    num++;
    this.innerHTML = str.substring(0, num);  // this 指向 window,無法正常工作
  }, 100);
}
 
// 解決方案一:使用變量保存 this
box1.onclick = function() {
  var _this = this;  // 保存當(dāng)前 this 指向
  var num = 0;
  setInterval(function() {
    num++;
    _this.innerHTML = str.substring(0, num);  // 使用保存的 this
  }, 100);
}
 
// 解決方案二:使用箭頭函數(shù)
box1.onclick = function() {
  var num = 0;
  setInterval(() => {  // 箭頭函數(shù)繼承外部 this
    num++;
    this.innerHTML = str.substring(0, num);  // this 指向 box1
  }, 100);
}

7. 箭頭函數(shù)中的 this

  • 箭頭函數(shù)沒有自己的 this,它捕獲定義時(shí)所在上下文的 this 作為自己的 this
  • 任何方法(call ()、apply ()、bind ())都無法改變箭頭函數(shù)的 this 指向
var obj = {
  name: "jack",
  say: () => {
    console.log(this.name);  // this 指向全局對(duì)象,而非 obj
  }
};

8. call ()、apply ()、bind () 中的 this

這三個(gè)方法用于改變函數(shù)中 this 的指向,是函數(shù)對(duì)象的方法。

8.1 call () 方法

語法:fun.call(thisArg, arg1, arg2, ...)

  • 立即調(diào)用函數(shù)
  • 第一個(gè)參數(shù)指定 this 指向
  • 后續(xù)參數(shù)以逗號(hào)分隔傳遞
function fun2(a, b) {
  console.log(a + b);  // 30
  console.log(this);   // 指向 box1 元素
}
 
box1.onclick = function() {
  fun2.call(this, 10, 20);  // 改變 this 指向?yàn)楫?dāng)前元素
}

8.2 apply () 方法

語法:fun.apply(thisArg, [argsArray])

  • 與 call () 類似,但參數(shù)通過數(shù)組傳遞
  • 立即調(diào)用函數(shù)
function fun2(a, b) {
  console.log(a + b);  // 30
  console.log(this);   // 指向 box1 元素
}
 
box1.onclick = function() {
  fun2.apply(this, [10, 20]);  // 參數(shù)以數(shù)組形式傳遞
}

實(shí)用案例:獲取數(shù)組最大值

var arr = [56, 76, 45, 34, 87, 98];
var max = Math.max.apply(null, arr);  // 98
// 等價(jià)于 ES6 的擴(kuò)展運(yùn)算符
var max = Math.max(...arr);  // 98

8.3 bind () 方法

語法:fun.bind(thisArg, arg1, arg2, ...)

  • 不會(huì)立即調(diào)用函數(shù),而是返回一個(gè)新函數(shù)
  • 新函數(shù)中的 this 被永久綁定到 bind () 的第一個(gè)參數(shù)
function fun() {
  console.log(this);  // 指向 box1 元素
}
 
box1.onclick = function() {
  var newFun = fun.bind(this);  // 綁定 this 指向
  newFun();  // 手動(dòng)調(diào)用新函數(shù)
}

call、apply、bind 的區(qū)別

  1. 調(diào)用方式

    • call () 和 apply () 會(huì)立即調(diào)用函數(shù)
    • bind () 不會(huì)立即調(diào)用,而是返回一個(gè)新函數(shù)
  2. 參數(shù)傳遞

    • call () 接收參數(shù)列表:func.call(this, arg1, arg2)
    • apply () 接收參數(shù)數(shù)組:func.apply(this, [arg1, arg2])
    • bind () 接收參數(shù)列表:func.bind(this, arg1, arg2)
  3. this 指向

    • 三者都能改變函數(shù)內(nèi)部 this 指向
    • 若第一個(gè)參數(shù)為 null 或 undefined,this 指向 window(非嚴(yán)格模式)

 到此這篇關(guān)于JavaScript this的多種場(chǎng)景使用的文章就介紹到這了,更多相關(guān)JavaScript this場(chǎng)景使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

昭苏县| 烟台市| 绥德县| 南开区| 临湘市| 河间市| 图木舒克市| 洪雅县| 万盛区| 邵武市| 稷山县| 高雄县| 自治县| 西盟| 泸水县| 惠来县| 永安市| 尉氏县| 博兴县| 昌邑市| 成都市| 盘锦市| 鹿泉市| 兴业县| 克什克腾旗| 苍溪县| 文安县| 克东县| 日照市| 汾西县| 石城县| 丰原市| 古田县| 孟津县| 武平县| 广饶县| 会昌县| 广德县| 西城区| SHOW| 鞍山市|