前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >带你入门 JavaScript ES6 (三)

带你入门 JavaScript ES6 (三)

作者头像
柳公子
发布2018-09-17 16:49:34
3670
发布2018-09-17 16:49:34
举报
文章被收录于专栏:PhpZendoPhpZendo

上一章我们学习了 for of 遍历和扩展字符语法。本章我们主要学习 ES6 中的箭头函数

箭头函数

更准确来说叫 箭头函数表达式。箭头函数余普通函数功能相同,但语法差别比较大。

看下例子:

代码语言:javascript
复制
// 普通函数
let info = ['name', 'age', 'address'].map(function (word){
    // 将首字母转换成大写
    return word.slice(0, 1).toUpperCase() + word.slice(1)
})

console.log(info)// ["Name", "Age", "Address"]

// 箭头函数
let info2 = ['name', 'age', 'address'].map(word => word.slice(0, 1).toUpperCase() + word.slice(1))

console.log(info2)// ["Name", "Age", "Address"]

1. 箭头函数语法

单独将上例中的函数部分摘取出来,箭头函数相比于普通函数少了 function(){} 这部分语法:

代码语言:javascript
复制
// 普通函数
function (word){
    // 将首字母转换成大写
    return word.slice(0, 1).toUpperCase() + word.slice(1)
}

// 箭头函数
word => word.slice(0, 1).toUpperCase() + word.slice(1)

总结下简单的箭头函数的语法定义:

step1: 删除普通函数的关键词 function

代码语言:javascript
复制
(word){
    return word.slice(0, 1).toUpperCase() + word.slice(1)
}

step2: 删除 圆括号()

代码语言:javascript
复制
word{
    return word.slice(0, 1).toUpperCase() + word.slice(1)
}

step3: 删除 花括号{},及关键词 return

代码语言:javascript
复制
word word.slice(0, 1).toUpperCase() + word.slice(1)

step4: 在参数列表与方法体之间加入箭头(=>)

代码语言:javascript
复制
word => word.slice(0, 1).toUpperCase() + word.slice(1)

2. 合法的箭头函数定义

代码语言:javascript
复制
// 1 无参数的箭头函数
let f = () => console.log("箭头函数")
console.log(f())

//*************************************
// 2. 一个参数的箭头函数,参数的圆括号部分可选

// 2.1 带圆括号
let f = (name) => console.log(name)
console.log(f('huliuqing'))

// 2.2 不带圆括号
let f = name => console.log(name)
console.log(f('huliuqing'))

//*************************************
// 3 多个参数的箭头函数,参数一定用圆括号包裹

let f = (name, age) => console.log(`${name} : ${age}`)
console.log(f('huliuqing', 18))

//*************************************
// 4 单行函数体与多行函数体的箭头函数

//  4.1 单行函数体将上面的示例

//  4.2 多行函数体将上面的示例
let f = (name, age) => {
    name = `hello ${name}`
    age  = age + 1

    return [name, age]
}

console.log(f('huliuqing', 18))

3. this 值

3.1 普通函数中的 this 值

① this 指向新的对象实例 当使用 new 关键词实例化对象时, this 执行对象实例

代码语言:javascript
复制
function Person() {
  console.log(this)
}

var p = new Person()

② this 指向被调用对象 当使用 call/apply 调用对象时,this 指向被调用对象

代码语言:javascript
复制
function getName() {
    // console.log(this)
    console.log(this.name)
}

let person = {
    name: 'huliuqing',
}

getName.call(person);// huliuqing

③ this 指向上下文的调用对象 如果函数由对象实例调用,此时 this 指向对象实例

代码语言:javascript
复制
let Person = {
    name: 'huliuqing',
    getName: function(){
        console.log(this.name)
    }
}

Person.getName()//Person 即为上下文环境,因而输出 huliuqing

④ this 指向全局对象或undefined 当调用函数时无上下文环境,在严格模式下 this 指向 undefined

代码语言:javascript
复制
function getName(){
    //console.log(this)
    console.log(this.name)
}

var name = 'huliuqing'

getName()// this 指向全局对象 Window,因而次数输出 huliuqing

严格模式下 this 为 undefined

代码语言:javascript
复制
function getName(){
    'use strict'

    console.log(this)
}

var name = 'huliuqing'

getName()// undefined

3.2 箭头函数中的 this 值

对于普通函数而言, this 的值是有函数如何被调用决定的,所以普通函数 this 的值比较随性,难以捕捉。为了解决这个问题,在箭头函数中 this 的值在任何情况下都是基于函数周围上下文,即函数的的 this 和函数外的 this 值一样

代码语言:javascript
复制
// 普通函数在 timeout 中的 name
var Person = {
    name: 'huliuqing',
    getName: function(){
        setTimeout(function(){
            console.log(this.name)
        }, 1000)
    }
}
Person.getName();// undefined

// 将 setTimeout 匿名函数转换成箭头函数后,匿名函数内的 this 值同 getName 函数的上下文一致(即 Person)
var Person = {
    name: 'huliuqing',
    getName: function(){
        setTimeout(() => console.log(this.name)
        , 1000)
    }
}
Person.getName();// huliuqing

// 将 getName 函数转换成箭头函数,getName 的 this 值依据上下文为 Window
var Person = {
    name: 'huliuqing',
    getName: () =>{
        console.log(this)
        setTimeout(() => console.log(this.name)
        , 1000)
    }
}
Person.getName();// 空,什么都没有

参考资料: MDN 箭头函数

this 原理

ES6 In Depth: Arrow functions

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年1月3日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 箭头函数
    • 1. 箭头函数语法
    • 2. 合法的箭头函数定义
    • 3. this 值
      • 3.1 普通函数中的 this 值
        • 3.2 箭头函数中的 this 值
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档