• <bdo id='PXEHI'></bdo><ul id='PXEHI'></ul>

    <legend id='PXEHI'><style id='PXEHI'><dir id='PXEHI'><q id='PXEHI'></q></dir></style></legend>
    <tfoot id='PXEHI'></tfoot>

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

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

    1. 关于JavaScript中的this指向问题总结篇

      时间:2023-12-08

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

          <tbody id='F2gHE'></tbody>
          <bdo id='F2gHE'></bdo><ul id='F2gHE'></ul>

          • <legend id='F2gHE'><style id='F2gHE'><dir id='F2gHE'><q id='F2gHE'></q></dir></style></legend>

              <tfoot id='F2gHE'></tfoot>

                <i id='F2gHE'><tr id='F2gHE'><dt id='F2gHE'><q id='F2gHE'><span id='F2gHE'><b id='F2gHE'><form id='F2gHE'><ins id='F2gHE'></ins><ul id='F2gHE'></ul><sub id='F2gHE'></sub></form><legend id='F2gHE'></legend><bdo id='F2gHE'><pre id='F2gHE'><center id='F2gHE'></center></pre></bdo></b><th id='F2gHE'></th></span></q></dt></tr></i><div id='F2gHE'><tfoot id='F2gHE'></tfoot><dl id='F2gHE'><fieldset id='F2gHE'></fieldset></dl></div>
              • 详细讲解“关于JavaScript中的this指向问题总结篇”的完整攻略

                一、背景及概述

                JavaScript的this关键字是指向函数执行的上下文,但是在不同的情况下,this所指向的上下文可能会有所不同。前端工程师在编写JavaScript代码时,常常会遇到this指向问题,如何准确地理解和掌握this指向的问题,是我们开发高质量JavaScript代码的重要一环。本篇攻略总结了关于JavaScript中的this指向问题的常见情况,并提供了相应的示例,帮助开发者更好地理解和掌握this指向问题。

                二、全局上下文中的this

                在全局上下文中,this指向全局对象。

                console.log(this === window); // true
                

                三、函数上下文中的this

                在函数上下文中,this可能指向多个不同的值,具体取决于函数的调用方式和函数自身的定义方式:

                1.函数体内部的this

                在函数内部,this的值为undefined。

                function test() {
                  console.log(this); // undefined
                }
                test();
                

                2.作为对象方法调用的this

                当函数作为对象的方法被调用时,this指向该对象。

                var obj = {
                  name: "John",
                  sayName: function() {
                    console.log(this.name);
                  }
                }
                obj.sayName(); // "John"
                

                3.作为构造函数调用的this

                当函数被用作构造函数调用时,this指向新创建的对象。

                function Person(name) {
                  this.name = name;
                }
                var john = new Person("John");
                console.log(john.name); // "John"
                

                4.通过apply、call、bind调用的this

                在使用apply、call、bind等方法调用函数时,this指向传入的第一个参数。

                function test(a, b) {
                  console.log(this);
                  console.log(a, b);
                }
                var obj = {name: "John"};
                test.call(obj, 1, 2); // {name: "John"}, 1, 2
                test.apply(obj, [1, 2]); // {name: "John"}, 1, 2
                var testFn = test.bind(obj);
                testFn(1, 2); // {name: "John"}, 1, 2
                

                5.箭头函数的this

                箭头函数不会有自己的this值,箭头函数中的this是取决于它所处的上下文环境的this值。

                var obj = {
                  name: "John",
                  sayName: () => { console.log(this.name); }
                }
                obj.sayName(); // undefined
                

                注意,箭头函数中的this指向并不是由调用方式决定的,而是由函数定义的位置所决定的,箭头函数所在的作用域中的this值会被继承到箭头函数中。

                四、总结

                通过本篇攻略的介绍,我们可以总结出下列几条关于JavaScript中的this指向问题的规律:

                1. 在全局上下文中,this指向全局对象。

                2. 在函数体内部,this的值为undefined。

                3. 在函数作为对象的方法被调用时,this指向该对象。

                4. 在构造函数中,this指向新创建的对象。

                5. 在使用apply、call、bind等方法调用函数时,this指向传入的第一个参数。

                6. 在箭头函数中,this指向所在作用域中的this值。

                以上规律的理解和掌握,有助于我们在JavaScript开发过程中更好地应对this指向问题。

                上一篇:JavaScript编码风格指南(中文版) 下一篇:javascript学习笔记(一) 在html中使用javascript

                相关文章

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

                <small id='2IIyj'></small><noframes id='2IIyj'>

                  • <bdo id='2IIyj'></bdo><ul id='2IIyj'></ul>