美文网首页
箭头函数中的this

箭头函数中的this

作者: 我的昵称在不在 | 来源:发表于2019-09-26 08:47 被阅读0次

<script>
    // 什么时候使用箭头
    // setTimeout(function () {
    //   console.log(this);
    // }, 1000)
    //
    // setTimeout(() => {
    //   console.log(this);
    // }, 1000)

    // 问题: 箭头函数中的this是如何查找的了?
    // 答案: 向外层作用域中, 一层层查找this, 直到有this的定义.
    const obj = {
      aaa() {
        setTimeout(function () {
          console.log(this); // window
        })

        setTimeout(() => {
          console.log(this); // obj对象
        })
      }
    }

    obj.aaa()


    const obj = {
      aaa() {
        setTimeout(function () {
          setTimeout(function () {
            console.log(this); // window
          })

          setTimeout(() => {
            console.log(this); // window
          })
        })

        setTimeout(() => {
          setTimeout(function () {
            console.log(this); // window
          })

          setTimeout(() => {
            console.log(this); // obj
          })
        })
      }
    }

    obj.aaa()
  </script>

相关文章

  • 九、箭头函数 ------ 2020-04-06

    1、箭头函数的创建: 2、箭头函数中没有arguments 3、箭头函数中没有自己的this

  • 箭头函数

    1,箭头函数定义 2,Es6 中箭头函数参数与返回值简写 补充 3,箭头函数中 this 指向 注:箭头函数中的t...

  • 一句话明白箭头函数中的this

    关于箭头函数中this值的问题,网上查查,会告诉你 “箭头函数的this固定化,箭头函数中的this绑定定义时所在...

  • ES学习笔记

    [摘抄自网络] 箭头函数 箭头函数中的this箭头函数看上去是匿名函数的一种简写,但实际上,箭头函数和匿名函数有个...

  • 箭头函数没有绑定this

    ==箭头函数没有绑定this== 不要把【箭头函数】和【箭头函数的定义函数】弄混淆 ecma262规范中明确规定,...

  • 函数的this是什么时候绑定的

    箭头函数是没有this的,箭头函数中的this只取决于包裹箭头函数的第一个普通函数的this。

  • 【Dart】函数

    声明函数 直接声明Dart中声明函数不需要function关键字 箭头函数+Dart中 的箭头函数中,函数体只能写...

  • 常见前端面试题

    箭头函数与普通函数的区别 箭头函数语法比普通函数更加简洁,但箭头函数中没有arguments,所以形参可以使用展开...

  • ES6-箭头函数

    箭头函数中的this ES6函数参数默认值 箭头函数不适用的场景

  • 箭头函数中的this

    箭头函数中的this箭头函数中的this绑定的是上下文的this对象,如下:var calculate = { ...

网友评论

      本文标题:箭头函数中的this

      本文链接:https://www.haomeiwen.com/subject/efbvuctx.html