JavaScript 中 this 指向问题

一、Bug 场景

在一个 JavaScript 的网页交互项目中,有一个构造函数定义了一个对象,该对象包含一个方法用于更新 DOM 元素的文本内容。同时,为了实现异步操作,在这个方法内部使用了 setTimeout 来模拟一些延迟任务。

二、代码示例

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>this指向问题</title>
</head>

<body>
    <div id="target"></div>

    <script>
        function DOMUpdater() {
            this.targetElement = document.getElementById('target');
            this.updateText = function () {
                setTimeout(function () {
                    this.targetElement.textContent = '更新后的文本';
                }, 1000);
            };
        }

        const updater = new DOMUpdater();
        updater.updateText();
    </script>
</body>

</html>

三、问题描述

  1. 预期行为 :等待 1 秒后,idtargetdiv 元素的文本内容应更新为 "更新后的文本"。
  2. 实际行为 :在控制台中会报错 Uncaught TypeError: Cannot set property 'textContent' of null。这是因为在 setTimeout 内部的回调函数中,this 的指向发生了变化。在非严格模式下,setTimeout 回调函数中的 this 指向全局对象(在浏览器环境中是 window),而不是 DOMUpdater 实例对象。由于 window 中没有 targetElement 属性,所以会导致 this.targetElementnull,进而引发错误。

四、解决方案

  1. 使用箭头函数 :箭头函数没有自己的 this,它的 this 继承自外层作用域,这样就可以保持 this 指向 DOMUpdater 实例对象。
html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>this指向问题 - 箭头函数解决</title>
</head>

<body>
    <div id="target"></div>

    <script>
        function DOMUpdater() {
            this.targetElement = document.getElementById('target');
            this.updateText = function () {
                setTimeout(() => {
                    this.targetElement.textContent = '更新后的文本';
                }, 1000);
            };
        }

        const updater = new DOMUpdater();
        updater.updateText();
    </script>
</body>

</html>
  1. 使用变量保存 this :在 updateText 方法内部,使用一个变量(通常命名为 selfthat)来保存 this 的值,然后在 setTimeout 回调函数中使用这个变量。
html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>this指向问题 - 变量保存this解决</title>
</head>

<body>
    <div id="target"></div>

    <script>
        function DOMUpdater() {
            this.targetElement = document.getElementById('target');
            this.updateText = function () {
                const self = this;
                setTimeout(function () {
                    self.targetElement.textContent = '更新后的文本';
                }, 1000);
            };
        }

        const updater = new DOMUpdater();
        updater.updateText();
    </script>
</body>

</html>
  1. 使用 bind 方法bind 方法可以创建一个新的函数,在这个新函数中,this 被绑定到指定的对象。
html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>this指向问题 - bind解决</title>
</head>

<body>
    <div id="target"></div>

    <script>
        function DOMUpdater() {
            this.targetElement = document.getElementById('target');
            this.updateText = function () {
                setTimeout(function () {
                    this.targetElement.textContent = '更新后的文本';
                }.bind(this), 1000);
            };
        }

        const updater = new DOMUpdater();
        updater.updateText();
    </script>
</body>

</html>
相关推荐
●VON5 分钟前
React Native for OpenHarmony:项目目录结构与跨平台构建流程详解
javascript·学习·react native·react.js·架构·跨平台·von
爱吃大芒果23 分钟前
Flutter for OpenHarmony 实战:mango_shop 路由系统的配置与页面跳转逻辑
开发语言·javascript·flutter
qq_1777673727 分钟前
React Native鸿蒙跨平台实现消息列表用于存储所有消息数据,筛选状态用于控制消息筛选结果
javascript·react native·react.js·ecmascript·harmonyos
沐雪架构师40 分钟前
LangChain 1.0 Agent开发实战指南
开发语言·javascript·langchain
2501_940007891 小时前
Flutter for OpenHarmony三国杀攻略App实战 - 战绩记录功能实现
开发语言·javascript·flutter
摘星编程1 小时前
React Native + OpenHarmony:自定义useEllipsis省略号处理
javascript·react native·react.js
这是个栗子1 小时前
【Vue代码分析】前端动态路由传参与可选参数标记:实现“添加/查看”模式的灵活路由配置
前端·javascript·vue.js
刘一说2 小时前
Vue 动态路由参数丢失问题详解:为什么 `:id` 拿不到值?
前端·javascript·vue.js
方也_arkling2 小时前
elementPlus按需导入配置
前端·javascript·vue.js
爱吃大芒果2 小时前
Flutter for OpenHarmony 实战: mango_shop 资源文件管理与鸿蒙适配
javascript·flutter·harmonyos