JS this指向问题

喜欢ヅ旅行 2022-12-25 11:56 298阅读 0赞

在绝大多数情况下,函数的调用方式决定了 this 的值。this 不能在执行期间被赋值,并且在每次函数被调用时 this 的值也可能会不同,所以总结 this 值的规律是有必要的

全局作用域 this 指向

无论是否在严格模式下,在全局作用域中 this 都指向全局对象

  1. console.log(this); // Window
  2. "use strict"
  3. console.log(this); // Window

私有作用域 this 指向

在函数内部,this 的值取决于函数调用的方式

通俗的来说,就是函数执行的时候,看函数名前面有没有 .

  • 如果函数调用时在某个对象上触发的(典型:xxx.fun()),会触发隐式绑定this 就是该对象,即 xxx
  • 如果函数调用时前面没有 . ,会触发默认绑定

    • 浏览器环境:Window
    • 严格模式:undefined

    function fn() {

    1. console.log(this);

    }
    var obj = {

    1. fn: fn,

    };

    obj.fn(); // obj
    fn(); // Window

    “use strict”
    obj.fn(); // obj
    fn(); // undefined

构造函数 this 指向(new绑定)

通过构造函数创建的实例,构造函数中的 this 指的就是当前类的实例

  • new 运算符做了这样一件事:将新对象和函数调用的 this 绑定起来

    function fn() {

    1. this.name = "lion";
    2. this.age = 13;
    3. console.log(this); // fn

    }
    var f = new fn();

    “use strict”;
    function fn() {

    1. this.name = "lion";
    2. this.age = 13;
    3. console.log(this); // fn

    }
    var f = new fn();

显示绑定 this 指向

通过 callapplybind 的方式,显示的指定 this 所指向的值

call 方法第一个参数是要绑定 this 的值,后面传入的是一个参数列表

  • 浏览器环境:不传参或传 null、undefined,this 都是 Window
  • 严格模式:不传参或传 undefined,this 都是 undefined,传 null,this 是 null

    function Fn(){

    1. console.log(this.name);

    }
    var person = {

    1. name: 'bird',
    2. Fn: Fn

    }
    var name = ‘dog’;
    var Hi = function(fn) {

    1. // 触发默认绑定,this 为 winodw
    2. fn(); // dog
    3. // 如果不希望绑定丢失
    4. fn.call(this); // bird

    }
    Hi.call(person, person.Fn);

call 硬绑定情况

强制把 foo 的 this 绑定到了 obj,无论之后如何调用 bar 之前的操作都不会被覆盖,它总会在 obj 上调用 foo

  1. function foo() {
  2. console.log(this.a);
  3. }
  4. var a = 1;
  5. var obj = {
  6. a: 2,
  7. };
  8. var bar = function () {
  9. foo.call(obj);
  10. };
  11. bar(); // 2
  12. setTimeout(bar, 100); // 2
  13. bar.call(window); // 2

自执行/回调函数 this 指向

其实和 私有作用域 this 指向 里的默认绑定是一个道理,与函数定义地方无关,取决于函数在哪调用

  • 浏览器环境:Window
  • 严格模式:undefined

    ~(function () {

    1. console.log(this);

    })(); // Window

    “use strict”;
    ~(function () {

    1. console.log(this);

    })(); // undefined

回调函数:一个函数作为参数传递给另一函数

  • 浏览器环境:Window
  • 严格模式:Window

这里需要注意:如果是 JS 内置方法,如 setTimeout ,回调函数内部 this 都会指向 Window,非内置方法严格模式下为 undefined

  1. setTimeout(function () {
  2. console.log(this);
  3. }, 100); // Window
  4. "use strict";
  5. setTimeout(function () {
  6. console.log(this);
  7. }, 100); // Window

元素绑定事件 this 指向

给元素绑定事件,当事件触发,函数执行的时候,里面的 this 就是当前绑定的元素

  • 浏览器环境:当前绑定事件的元素
  • 严格模式:当前绑定事件的元素

    el.onclick = function () {

    1. console.log(this);

    }; // el

    “use strict”;
    el.onclick = function () {

    1. console.log(this);

    }; // el

箭头函数 this

箭头函数中没有 this,也没有 this 绑定机制,还没有 arguments

  1. function fn1() {
  2. this.a = 55;
  3. return {
  4. a: 10,
  5. b: function () {
  6. return this;
  7. },
  8. };
  9. }
  10. var f1 = new fn1();
  11. console.log(f1.b()); // {a: 10, b: ƒ}

如果在箭头函数中用 this,this 仅仅相当于一个变量,会向上级作用域去找

  1. function fn2() {
  2. this.a = 55;
  3. return {
  4. a: 10,
  5. b: () => {
  6. return this;
  7. },
  8. };
  9. }
  10. var f2 = new fn2();
  11. console.log(f2.b()); // fn2 { a: 55 }

发表评论

表情:
评论列表 (有 0 条评论,298人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JS中的this指向问题

    this(上下文对象) 我们每次调用函数时,解析器都会将一个上下文对象作为隐含的参数传递进函数。 使用this来引用上下文对象,根据函数的调用形式不同,this的值也

    相关 JS this指向问题

    在绝大多数情况下,函数的调用方式决定了 `this` 的值。`this` 不能在执行期间被赋值,并且在每次函数被调用时 `this` 的值也可能会不同,所以总结 `this`

    相关 JS干货分享—-this指向问题

    JS干货分享—-this指向问题 平时用this有些混乱,所以写个总结。 没有箭头函数之前,我们说this就是函数运行时所在的环境对象,但是在箭头函数中this就是定义时...