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

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

    2. <legend id='ZyVMW'><style id='ZyVMW'><dir id='ZyVMW'><q id='ZyVMW'></q></dir></style></legend>

      1. 浅谈JavaScript中this的指向更改

        浅谈JavaScript中this的指向更改
          <tbody id='NflcM'></tbody>
          <i id='NflcM'><tr id='NflcM'><dt id='NflcM'><q id='NflcM'><span id='NflcM'><b id='NflcM'><form id='NflcM'><ins id='NflcM'></ins><ul id='NflcM'></ul><sub id='NflcM'></sub></form><legend id='NflcM'></legend><bdo id='NflcM'><pre id='NflcM'><center id='NflcM'></center></pre></bdo></b><th id='NflcM'></th></span></q></dt></tr></i><div id='NflcM'><tfoot id='NflcM'></tfoot><dl id='NflcM'><fieldset id='NflcM'></fieldset></dl></div>
              <bdo id='NflcM'></bdo><ul id='NflcM'></ul>

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

                • <legend id='NflcM'><style id='NflcM'><dir id='NflcM'><q id='NflcM'></q></dir></style></legend>
                • <tfoot id='NflcM'></tfoot>

                  浅谈JavaScript中this的指向更改

                  在JavaScript中,函数的 this 关键字指向的是调用函数的对象,而不同的函数调用方式会影响 this 的指向。本文将详细讨论如何通过不同的方式来更改 this 的指向。

                  1. 使用 call() 方法

                  call() 方法可以传递一个对象,并将其作为函数中的 this。例如:

                  let obj = { name: "Alice" };
                  let sayHello = function() {
                    console.log("Hello, " + this.name);
                  };
                  sayHello.call(obj); // Hello, Alice
                  

                  在上面的例子中,我们将 obj 对象作为 sayHello 函数中的 this。调用 sayHello.call(obj) 执行函数时,this 就指向了 obj 对象。

                  1. 使用 apply() 方法

                  apply() 方法与 call() 方法的作用相同,区别之处在于 apply() 接受的是一个数组形式的参数列表。

                  例如:

                  let obj = { name: "Bob" };
                  let sayHello = function(greeting) {
                    console.log(greeting + ", " + this.name);
                  };
                  sayHello.apply(obj, ["Hi"]); // Hi, Bob
                  

                  在上面的例子中,我们调用了 sayHello 函数,并传递了一个数组作为参数,数组中包含一个字符串 "Hi"。apply() 方法将 obj 对象作为函数中的 this,并传递了参数列表,最终输出了 "Hi, Bob"。

                  1. 使用 bind() 方法

                  bind() 方法会创建一个新函数,并将指定的对象作为新函数的 this,而不改变原始函数的 this 指向。

                  例如:

                  let obj = { name: "Charlie" };
                  let sayHello = function() {
                    console.log("Hello, " + this.name);
                  };
                  let newSayHello = sayHello.bind(obj);
                  newSayHello(); // Hello, Charlie
                  

                  在上面的例子中,我们创建了一个新函数 newSayHello,并将 obj 对象作为新函数的 this。由于 bind() 方法不会改变原始函数的 this 指向,因此原始函数 sayHello 的 this 仍为全局对象。而新函数 newSayHello 的 this 指向了 obj 对象。

                  通过以上示例,我们可以看出,在 JavaScript 中使用 call()、apply() 和 bind() 方法可以很容易地更改函数中的 this 指向。在实际开发中,我们可以根据实际需求选择适合的方法来更改 this 的指向。

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

                  相关文档推荐

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

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

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