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

JavaScript中的this問(wèn)題超詳細(xì)總結(jié)

 更新時(shí)間:2025年06月05日 10:07:23   作者:言者非也  
這篇文章主要介紹了JavaScript中this問(wèn)題的相關(guān)資料,this的指向規(guī)則涵蓋全局、對(duì)象方法、構(gòu)造函數(shù)、箭頭函數(shù)及事件處理中的上下文,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、this的指向問(wèn)題

1、全局環(huán)境的this

在全局環(huán)境中(即不在任何函數(shù)內(nèi)部),this 指向全局對(duì)象:

  • 在瀏覽器環(huán)境中,this 指向 window 對(duì)象。
console.log(this); // 瀏覽器環(huán)境中輸出 window
  • 在 Node.js 環(huán)境中,this 指向 global 對(duì)象(但在嚴(yán)格模式下,this 是 undefined)。
"use strict";
console.log(this); // 輸出 undefined

2、對(duì)象方法中的this

當(dāng) this 出現(xiàn)在對(duì)象的方法中時(shí),this 指向調(diào)用該方法的對(duì)象。

const obj = {
  name: 'Alice',
  sayName: function () {
    console.log(this.name);
  }
};

obj.sayName(); // 輸出 'Alice'

3、構(gòu)造函數(shù)中的 this

在構(gòu)造函數(shù)中,this 指向正在創(chuàng)建的實(shí)例對(duì)象。

function Person(name) {
  this.name = name;
}

const person1 = new Person('Bob');
console.log(person1.name); // 輸出 'Bob'

4、箭頭函數(shù)中的this

箭頭函數(shù)不會(huì)創(chuàng)建自己的 this,它會(huì)捕獲其所在上下文的 this 值。

const obj = {
  name: 'Alice',
  sayName: function () {
    const arrowFunc = () => {
      console.log(this.name);
    };
    arrowFunc();
  }
};

obj.sayName(); // 輸出 'Alice'
//箭頭函數(shù) arrowFunc 中的 this 繼承自 sayName 方法中的 this,即 obj

5、事件處理器中的 this

在 DOM 事件處理器中,this 默認(rèn)指向觸發(fā)事件的 DOM 元素

const button = document.querySelector('button');
button.addEventListener('click', function () {
  console.log(this); // 指向 button 元素
});

如果使用箭頭函數(shù)作為事件處理器,this 將不再指向 DOM 元素,而是繼承自外部作用域的 this

const button = document.querySelector('button');
button.addEventListener('click', () => {
  console.log(this); // 指向外部作用域的 this(可能是 window 或 undefined,取決于嚴(yán)格模式)
});

6、 類中的 this

在類中,this 的行為與構(gòu)造函數(shù)類似,指向當(dāng)前實(shí)例對(duì)象。

class Person {
  constructor(name) {
    this.name = name;
  }

  sayName() {
    console.log(this.name);
  }
}

const person = new Person('Dave');
person.sayName(); // 輸出 'Dave'

二、回調(diào)函數(shù)中的this問(wèn)題

this的指向在回調(diào)函數(shù)中會(huì)變得復(fù)雜,而this的指向問(wèn)題也主要集中于回調(diào)函數(shù),因?yàn)榛卣{(diào)函數(shù)的this與上下文的調(diào)用有關(guān),因此另開(kāi)一個(gè)章節(jié)重新進(jìn)行分析:

1、普通函數(shù)作為回調(diào)函數(shù)

當(dāng)普通函數(shù)被用作回調(diào)函數(shù)時(shí),this的指向通常取決于調(diào)用該函數(shù)的對(duì)象,而不是定義它的對(duì)象。默認(rèn)情況下,this在非嚴(yán)格模式下指向全局對(duì)象(瀏覽器中是 window),在嚴(yán)格模式下指向 undefined。

function myCallback() {
  console.log(this);
}

const obj = {
  method: function() {
    [1, 2, 3].forEach(myCallback); // myCallback 作為回調(diào)函數(shù)
  }
};

obj.method();
// 輸出:
// 在非嚴(yán)格模式下,輸出 window(瀏覽器環(huán)境)
// 在嚴(yán)格模式下,輸出 undefined

2、對(duì)象方法作為回調(diào)函數(shù)

如果對(duì)象的方法被用作回調(diào)函數(shù),this 通常不會(huì)指向該對(duì)象,除非顯式綁定。

const obj = {
  name: 'Alice',
  sayName: function() {
    console.log(this.name);
  }
};

[1, 2, 3].forEach(obj.sayName);
// 輸出:
// undefined(因?yàn)?this 不再指向 obj)
// 或者報(bào)錯(cuò)(如果 this.name 被訪問(wèn)且 this 是 undefined 或 window)

3、 事件處理器中的回調(diào)函數(shù)

在 DOM 事件處理器中,this默認(rèn)指向觸發(fā)事件的 DOM 元素。如果使用箭頭函數(shù),this將繼承自外部作用域的 this。

const button = document.querySelector('button');

button.addEventListener('click', function() {
  console.log(this); // 指向 button 元素
});

button.addEventListener('click', () => {
  console.log(this); // 指向外部作用域的 this(可能是 window 或 undefined,取決于嚴(yán)格模式)
});

4、async 回調(diào)函數(shù)中的 this

在 async 函數(shù)或 Promise 的回調(diào)中,this 的指向與普通函數(shù)相同,取決于調(diào)用方式。

const obj = {
  name: 'Alice',
  fetchData: async function() {
    const data = await new Promise((resolve) => {
      setTimeout(() => {
        resolve('Data fetched');
      }, 1000);
    });
    console.log(this.name); // 這里的 this 需要正確綁定
  }
};

// 使用箭頭函數(shù)或 bind 確保 this 指向 obj
obj.fetchData().then(() => {}); // 正常輸出 'Alice'

// 如果在另一個(gè)上下文中調(diào)用 fetchData,需要綁定 this
const fetch = obj.fetchData;
fetch.call(obj); // 或者使用 bind

三、改變this指向的方法

1、把關(guān)鍵字轉(zhuǎn)為變量

案例:

let xiaoming = {
    age: 18,
    introduce: function() {
        setTimeout(function() {
            console.log(this.age)
        }, 1000)
    }
}

xiaoming.introduce();

解決方案:

let xiaoming = {
    age: 18,
    introduce: function() {
        let a = this;          //用一個(gè)變量a來(lái)儲(chǔ)存
        setTimeout(function() {
            console.log(a.age)  //此處變?yōu)檎{(diào)用a
        }, 1000)
    }
}

xiaoming.introduce();

2、綁定函數(shù)的this

案例:

class MyCom {
    constructor() {
        this.value = 100;
    }
    getValue() {
        return this.value;
    }
}

let mc = new MyCom();
console.log(mc.getValue());  // 結(jié)果: 100

如果修改為下面代碼:

class MyCom {
    constructor() {
        this.value = 100;
    }
    getValue() {
        return this.value;
    }
}

let btn = {
    value: 200
}

let mc = new MyCom();
btn.getValue = mc.getValue;
console.log(btn.getValue());  // 結(jié)果: 200。因?yàn)檎{(diào)用者變?yōu)榱薭tn

如果希望函數(shù)永遠(yuǎn)指向MyCom的實(shí)例對(duì)象:

class MyCom {
    constructor() {
        this.value = 100;
        this.getValue = this.getValue.bind(this);
    }
    getValue() {
        return this.value;  //此時(shí)this被鎖定為MyCom的實(shí)例對(duì)象
    }
}

let btn = {
    value: 200
}

let mc = new MyCom();
btn.getValue = mc.getValue;
console.log(btn.getValue());  // 結(jié)果: 100

bind方法的使用:每個(gè)函數(shù)本身都有一個(gè)bind方法,作用就是用來(lái)固定函數(shù)的this指向。其不會(huì)對(duì)函數(shù)本身進(jìn)行任何改造,實(shí)際上它會(huì)生成一個(gè)新的函數(shù)并返回(需要我們接收)如下:

function test() {
}

let newTest = test.bind(xiaoming);
newTest();  // this永遠(yuǎn)指向xiaoming

3、箭頭函數(shù)(上文有提過(guò))

4、使用call/apply代替bind

案例:

在上面中使用bind太過(guò)繁瑣,可以使用call/apply解決問(wèn)題:

總結(jié)

到此這篇關(guān)于JavaScript中的this問(wèn)題的文章就介紹到這了,更多相關(guān)js this問(wèn)題總結(jié)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 分享10個(gè)常見(jiàn)的JavaScript前端手寫功能

    分享10個(gè)常見(jiàn)的JavaScript前端手寫功能

    這篇文章主要分享10個(gè)常見(jiàn)的前端手寫功能,防抖、節(jié)流、深拷貝、異步控制并發(fā)數(shù)、繼承等功能技巧,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-02-02
  • javascript實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)程序

    javascript實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)程序

    這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 最新評(píng)論

    高唐县| 宁海县| 扎赉特旗| 石家庄市| 台中县| 安龙县| 东辽县| 磐安县| 泾源县| 盐亭县| 正镶白旗| 简阳市| 大方县| 曲松县| 阳高县| 乌拉特中旗| 平阳县| 昔阳县| 朝阳区| 崇文区| 黄陵县| 平顺县| 桐城市| 宣城市| 吕梁市| 乌审旗| 呼图壁县| 平邑县| 泸溪县| 永胜县| 益阳市| 拉萨市| 新化县| 凌云县| 个旧市| 如东县| 阜康市| 余庆县| 闸北区| 习水县| 徐州市|