<small id='aOap5'></small><noframes id='aOap5'>

<tfoot id='aOap5'></tfoot>

        <bdo id='aOap5'></bdo><ul id='aOap5'></ul>

        <legend id='aOap5'><style id='aOap5'><dir id='aOap5'><q id='aOap5'></q></dir></style></legend>
        <i id='aOap5'><tr id='aOap5'><dt id='aOap5'><q id='aOap5'><span id='aOap5'><b id='aOap5'><form id='aOap5'><ins id='aOap5'></ins><ul id='aOap5'></ul><sub id='aOap5'></sub></form><legend id='aOap5'></legend><bdo id='aOap5'><pre id='aOap5'><center id='aOap5'></center></pre></bdo></b><th id='aOap5'></th></span></q></dt></tr></i><div id='aOap5'><tfoot id='aOap5'></tfoot><dl id='aOap5'><fieldset id='aOap5'></fieldset></dl></div>

        详解JS中定时器setInterval和setTImeout的this指向问题

        下面我将用Markdown语言,来给大家分享一篇关于JS中setInterval和setTimeout的this指向问题的详解攻略。

        <small id='05El6'></small><noframes id='05El6'>

        <i id='05El6'><tr id='05El6'><dt id='05El6'><q id='05El6'><span id='05El6'><b id='05El6'><form id='05El6'><ins id='05El6'></ins><ul id='05El6'></ul><sub id='05El6'></sub></form><legend id='05El6'></legend><bdo id='05El6'><pre id='05El6'><center id='05El6'></center></pre></bdo></b><th id='05El6'></th></span></q></dt></tr></i><div id='05El6'><tfoot id='05El6'></tfoot><dl id='05El6'><fieldset id='05El6'></fieldset></dl></div>
          <tbody id='05El6'></tbody>

            1. <legend id='05El6'><style id='05El6'><dir id='05El6'><q id='05El6'></q></dir></style></legend>
                  <bdo id='05El6'></bdo><ul id='05El6'></ul>
                • <tfoot id='05El6'></tfoot>

                  下面我将用Markdown语言,来给大家分享一篇关于JS中setIntervalsetTimeoutthis指向问题的详解攻略。

                  一、问题描述

                  在使用setIntervalsetTimeout的时候,我们经常会遇到this指向问题,导致定时器中的代码无法访问到原始对象中的属性和方法。

                  二、原因分析

                  在JS中,setIntervalsetTimeout执行回调函数时,会通过全局对象window来执行,此时的this指向window对象,而不是原始的对象,导致回调函数无法访问原始对象中的属性和方法。

                  三、解决方案

                  1. 使用箭头函数

                  在ES6中引入了箭头函数,通过箭头函数可以解决this指向问题。箭头函数中的this是词法作用域,指向函数外部的this值。

                  class MyClass{
                      constructor() {
                          this.num = 0;
                          setInterval(() => {
                              this.num++;
                              console.log(this.num);
                          }, 500);
                      }
                  }
                  
                  let myClass = new MyClass();
                  

                  在上述代码中,我们使用了ES6中的class来定义一个类MyClass,在这个类的构造函数中,我们使用箭头函数来访问this,从而确保了回调函数中的this指向原始对象MyClass

                  2. 使用bind()方法

                  在JS中,可以使用Function.prototype.bind()方法改变函数的this指向,通过该方法可以将回调函数中的this指向原始对象。

                  class MyClass{
                      constructor() {
                          this.num = 0;
                          setInterval(function() {
                              this.num++;
                              console.log(this.num);
                          }.bind(this), 500);
                      }
                  }
                  
                  let myClass = new MyClass();
                  

                  在上述代码中,我们使用了Function.prototype.bind()方法将回调函数中的this指向了原始对象MyClass,这样回调函数中就能访问原始对象中的属性和方法了。

                  四、示例说明

                  示例一

                  let obj = {
                      count: 0,
                      start: function() {
                          setInterval(function() {
                              this.count++;
                              console.log(this.count);
                          }, 1000);
                      }
                  };
                  
                  obj.start();
                  

                  在上述代码中,我们定义了一个对象obj,其中有一个属性count,和一个方法start。在该对象的start方法中,我们使用setInterval来定时执行回调函数,每隔1秒将count加1并输出。但实际上该代码运行时会提示this.countundefined,这是由于回调函数中的this指向了window对象。

                  使用箭头函数或Function.prototype.bind()方法都可以解决该问题。这里我们演示使用Function.prototype.bind()来解决问题:

                  let obj = {
                      count: 0,
                      start: function() {
                          setInterval(function() {
                              this.count++;
                              console.log(this.count);
                          }.bind(this), 1000);
                      }
                  };
                  
                  obj.start();
                  

                  在该代码中,我们使用了Function.prototype.bind()将回调函数中的this指向了原始对象obj,从而实现了定时器中count属性的访问。

                  示例二

                  class MyClass{
                      constructor() {
                          this.num = 0;
                          setInterval(function() {
                              this.num++;
                              console.log(this.num);
                          }, 500);
                      }
                  }
                  
                  let myClass = new MyClass();
                  

                  在上述代码中,我们使用了ES6中的class定义了一个类MyClass,在该类的构造函数中,我们使用setInterval来定时执行回调函数,每隔0.5秒将num加1并输出。但是执行该代码时,会提示this.numundefined,这是由于回调函数中的this指向了window对象。

                  使用箭头函数或Function.prototype.bind()方法都可以解决该问题。这里我们演示使用箭头函数来解决问题:

                  class MyClass{
                      constructor() {
                          this.num = 0;
                          setInterval(() => {
                              this.num++;
                              console.log(this.num);
                          }, 500);
                      }
                  }
                  
                  let myClass = new MyClass();
                  

                  在该代码中,我们使用ES6的箭头函数符号来定义回调函数,从而确保了回调函数中的this指向原始对象MyClass,实现了定时器中num属性的访问。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  layui实现图片上传成功后回显点击放大图片功能,html代码部分: !-- html代码--div class="layui-form-item" label class="layui-form-label"上传图片/label div class="layui-input-block" button type="button" class="layui-btn" id="license-auth-letter-
                  Layui实现数据表格中鼠标悬停图片放大离开时恢复原图的效果,最终效果如下图所示: 实现代码如下,在done函数中调用hoverOpenImg方法 var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '/postData', //width : 150
                  我们在用到layui时候,需要点击文本输入框调起弹出选择框并选择内容,这个要怎么操作呢?以下两种方法可以参考: 1、点击名称,弹出信息弹框,选择表格中的某一行,实现效果如下: html页面代码 !--计量器具弹出层-- div id="equipment" lay-filter="equipmen
                  https的网站如果引用百度地图,会出现加载不了的问题,这是因为涉及到跨域问题,网站是https的,但是引用百度地图的是http的,这个要怎么操作呢? 比如我引用的地址:http://api.map.baidu.com/api?v=2.0ak=AK显示 后来看了一下,少了一个s=1字段,加一下s=1
                  做小程序项目的时候,客户提了一个功能需求优化,就是长按文字需要复制全部内容,因为有的手机支持全选复制,有的手机不支持全选复制。 通过设置系统剪贴板的内容和获取系统剪贴板的内容实现复制功能 html相关代码: van-field value="{{form.contactPhone}}"
                  由于项目功能需要,要实现对table中的行实现拖拽排序功能,找来找去发现Sortable.js能很好的满足这个需求,而且它还是开源的,于是乎就开始学习使用Sortable.js 特点 轻量级但功能强大 移动列表项时有动画 支持触屏设备和大多数浏览器(IE9及以下除外) 支持
                  1. <small id='pS1V5'></small><noframes id='pS1V5'>

                        <bdo id='pS1V5'></bdo><ul id='pS1V5'></ul>
                        <legend id='pS1V5'><style id='pS1V5'><dir id='pS1V5'><q id='pS1V5'></q></dir></style></legend>

                            <tbody id='pS1V5'></tbody>
                        1. <i id='pS1V5'><tr id='pS1V5'><dt id='pS1V5'><q id='pS1V5'><span id='pS1V5'><b id='pS1V5'><form id='pS1V5'><ins id='pS1V5'></ins><ul id='pS1V5'></ul><sub id='pS1V5'></sub></form><legend id='pS1V5'></legend><bdo id='pS1V5'><pre id='pS1V5'><center id='pS1V5'></center></pre></bdo></b><th id='pS1V5'></th></span></q></dt></tr></i><div id='pS1V5'><tfoot id='pS1V5'></tfoot><dl id='pS1V5'><fieldset id='pS1V5'></fieldset></dl></div>
                            <tfoot id='pS1V5'></tfoot>