<legend id='sVkwW'><style id='sVkwW'><dir id='sVkwW'><q id='sVkwW'></q></dir></style></legend>
  • <tfoot id='sVkwW'></tfoot>
        <bdo id='sVkwW'></bdo><ul id='sVkwW'></ul>

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

        <i id='sVkwW'><tr id='sVkwW'><dt id='sVkwW'><q id='sVkwW'><span id='sVkwW'><b id='sVkwW'><form id='sVkwW'><ins id='sVkwW'></ins><ul id='sVkwW'></ul><sub id='sVkwW'></sub></form><legend id='sVkwW'></legend><bdo id='sVkwW'><pre id='sVkwW'><center id='sVkwW'></center></pre></bdo></b><th id='sVkwW'></th></span></q></dt></tr></i><div id='sVkwW'><tfoot id='sVkwW'></tfoot><dl id='sVkwW'><fieldset id='sVkwW'></fieldset></dl></div>
      1. JS 中在严格模式下 this 的指向问题

        JS 中的 this 表示函数执行时所在的上下文对象,在不同的情况下,this 指向的对象是不同的,这是 JS 中一个比较重要,也比较复杂的概念。
          <bdo id='MAxAJ'></bdo><ul id='MAxAJ'></ul>

        • <tfoot id='MAxAJ'></tfoot>

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

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

                  JS 中的 this 表示函数执行时所在的上下文对象,在不同的情况下,this 指向的对象是不同的,这是 JS 中一个比较重要,也比较复杂的概念。

                  在严格模式下,this 指向的对象与非严格模式下不同。下面我们通过两个示例来详细讲解在严格模式下 this 的指向问题。

                  示例一

                  'use strict';
                  
                  function showThis() {
                    console.log(`in showThis, this is ${this}`);
                  }
                  
                  showThis(); // TypeError: this is undefined
                  

                  在函数 showThis 内部,由于是在严格模式下执行,this 并没有指向全局对象,而是 undefined,因为严格模式下,函数内部的 this 不能默认指向全局对象。

                  示例二

                  'use strict';
                  
                  const person = {
                    firstName: '张',
                    lastName: '三',
                    fullName: function() {
                      console.log(`当前对象中的 this: ${this}`);
                      function getName() {
                        console.log(`函数中的 this: ${this}`);
                        return this.firstName + this.lastName;
                      }
                      return getName();
                    }
                  }
                  
                  person.fullName(); // 函数中的 this: undefined
                                     // Uncaught TypeError: Cannot read property 'firstName' of undefined
                  

                  在函数 fullName 中调用函数 getName,其中 getName 函数是一个独立函数,它的执行环境并不是在 person 对象下。由于在严格模式下,独立函数内部的 this 不会指向全局对象,所以此时的 this 是 undefined,导致后面的 firstName 和 lastName 操作都会报错。

                  为了解决这个问题,我们可以使用 call、apply 或 bind 方法明确指定 this 的指向,或者使用箭头函数来绑定上下文。

                  'use strict';
                  
                  const person = {
                    firstName: '张',
                    lastName: '三',
                    fullName: function() {
                      const _this = this;
                      function getName() {
                        console.log(`函数中的 this: ${this}`);
                        return _this.firstName + _this.lastName;
                      }
                      return getName.call(_this);
                    }
                  }
                  
                  person.fullName(); // 函数中的 this: { firstName: '张', lastName: '三', fullName: [Function: fullName] }
                                     // 张三
                  

                  以上就是关于在严格模式下 this 的指向问题的详细说明,如果熟悉了这个概念,就可以更好地理解 JS 中的许多复杂问题了。

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

                  相关文档推荐

                  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及以下除外) 支持
                    <tbody id='D8WXL'></tbody>
                    <bdo id='D8WXL'></bdo><ul id='D8WXL'></ul>
                  • <legend id='D8WXL'><style id='D8WXL'><dir id='D8WXL'><q id='D8WXL'></q></dir></style></legend>

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

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