ES6 箭头函数

2018-12-06  本文已影响0人  人话博客

箭头函数在 ES6 的时候推出了.

它的语法有点类似于 .NET 里的 lamdba 表达式.

基本语法

(参数类表)=>{函数体}

let sum = (a,b) => a + b

一般用于匿名回调函数使用

app.post(url,(response)=>{
    //.....
})

// 等价于
app.post(url,(response)=>{
    //...
})

抛开this关键字,箭头函数和普通的函数是完全等价的.
它俩可以互换使用.


this和箭头函数

JavaScript中,我们使用this有这么几种情况.

function UserCreator(name,age) {
      this.name = name
      this.age = age
    }
    UserCreator.prototype.showInfo = function() {
      console.log(`${this.name} -- ${this.age}`)
    }

let user = new UserCreator('张三',24)
user.showInfo()
let obj = {
      message:'message',

      show:function() {
        console.log(this.message)
      }
    }

obj.show()
 function isWindow() {
      console.log(this === window)
    }

isWindow()
obj.show.call({message:'我是另外一个message'})

--

其实说白了,所有的 JavaScript 函数都是不一个独立存在的.
它们的调用都会传入一个对象.
对于 obj.method() ===> method(obj)
对于 method() ===> method(window)'

也就是说,普通函数的this,我们可以在定义的时候如果不去改它,那么它们就是符合我们预期的指向.但我们也可以去手动的修改this的指向,通过 call/apply


当箭头函数遇上了this.

看了很多博客讲的这些知识,一上来就给结论,然后证明结论.让我总感觉有点不知其所以然.

所以,决定开始自己慢慢琢磨这个点.

let arrowFn = () => {
      console.log(this === window)
    }

arrowFn()

  UserCreator.prototype.arrowFn = () => {
      console.log(this === window)
    }
let obj = {
      show2:()=>{
        console.log(this+'----')
      }
    }

所以,只要涉及到了this关键字,那么箭头函数和普通函还是有很大的区别的.

箭头函数的this和箭头函数所声明的词法作用域相关.

箭头函数,没有this,它的this是从当前此法作用域继承过来的.

UserCreator.prototype.arrowFn = 
// 当前作用域为 window
() => {
      console.log(this === window)
    }

如果这么看这一段代码的话,this === window好像比较好理解.

关键在于

let obj = {
      show2:()=>{
        console.log(this===window) //true
      },

一个比较常见的解释箭头函数和普通函数this的代码.

let obj2 = {
      message: 'message',
      show: function () {
        console.log(this.message) // 没问题
        setTimeout(function(){
          console.log(this.message) // 由于这个回调函数,没有显示的指向obj.method() 所以,它里面的this指向的是window对象. // 输出结构是 undefiend
        },1000);
      }
    }

    obj2.show()
undefined

但是,如果改成箭头函数,this,就由它外围的词法总作用域决定.而外围的this,指向的是obj2对象.

  let obj2 = {
      message: 'message',
      show:function(){
        setTimeout(() => {
          console.log(this.message)
        }, 1000);
      }
    }
message

当写完第二个箭头函数的 DEMO 后,突然有点对箭头函数 this的指向有点眉目了.

let obj2 = {
      message: 'message',
      getMessage:function(){
        // 词法作用域,this == obj2
        (()=>{
          console.log(this.message)
        })()
    }
}

结果符合预期.

在来一个例子证明自己的想法.

function doSomething() {
    return () => {
      console.log(this.message)
    }
  }  

  doSomething.call({message:'我是dosomething的函数调用的对象,我里面有个箭头函数,箭头函数的this就是我,所以可以用this.message'})()
15440780553014.jpg

如果上述猜测没错的话,箭头函数一直嵌套下去结果会如何呢?

  function doSomething2() {
    console.log(this.name)
    // 箭头函数继承this
    ;
    (()=>{
      console.log(this.name)
      this.name = '我被第一个箭头函数改了,在套一层'
      ;
      (()=>{
        console.log(this.name)
        this.name = '我被第二个箭头函数改了,还套一层'
        console.log(this.name)
      })()
    })()
  }

15440790413904.jpg

在来一个例子证明:箭头函数的this继承自最近的把它嵌套的function里的this指向.

let obj3 = {
    message:'message',
    show:function() { // this === obj3
      return function() {
        console.log(this === window) //this === window
        return () => { // 最近的 function --> this === window
          console.log(this.message) // 所以结果就是: undefined
        }
      }
    }
  }

  obj3.show()()()
15440800378738.jpg

总结:

15440783115731.jpg 15440783996788.jpg

用另一个函数把箭头函数包裹起来,外围的函数this是谁,箭头函数的this就是谁。

上一篇 下一篇

猜你喜欢

热点阅读