美文网首页
ES6 - 箭头函数

ES6 - 箭头函数

作者: ElricTang | 来源:发表于2019-10-08 22:56 被阅读0次

箭头函数有点类似于匿名函数,而且更加简洁。

一. 语法:
function add(x,y){
    return x + y;
}
console.log(add(1,2));// 3
  • 基本语法:在(){}之间添加=>
let add = (x,y) =>{
    return x + y;
};
console.log(add(1,2));// 3
  • 当返回值只有一句表达式时可以省略{}return
let add = (x,y) => x + y ;
console.log(add(1,2));// 3
  • 当参数只有一个时,可以省略()
let arr = [1,3,4].map(e => {
    return e + 1;
})
console.log(arr);// [ 2, 4, 5 ]
  • 无参数时也应该写()
setTimeout(()=>{
    console.log('无参数')
},500);// 无参数
  • 返回值为对象字面量时,如果简写需要添加()
let show = ({name,age}) => ({name:name,age:age});
console.log(show({name:'tom',age:21}));// { name: 'tom', age: 21 }
  • 支持参数数组解构赋值与对象解构赋值
let test1 = ([a,b,c]) => {
    return a + b + c;
}

let test2 = ({name,age,hobby}) => {
    return `my name is ${name} , i am ${age} years old , hobby is ${hobby}`;
}

console.log(test1([1,2,3]));// 6
console.log(test2({name:'tom',age:21,hobby:'game'}));
// my name is tom , i am 21 years old , hobby is game
二. 主要特点:
  • 不能作为构造函数
var Foo = () => {};
let foo = new Foo();// TypeError: Foo is not a constructor
  • 不绑定this(这是与匿名函数最大的区别,也是箭头函数重要的作用)
function Person(name){
    this.name = name;
    this.say = function(){
        return function(){
            console.log(this.name);
        }       
    }
}
let person = new Person('tom');
person.say()();// undefined

由于this绑定问题(函数创建了自己的this),得不到期望结果

function Person(name){
    this.name = name;
    this.say = function(){
        let that = this;
        return function(){
            console.log(that.name);
        }       
    }
}
let person = new Person('tom');
person.say()();// tom

在没有箭头函数之前,我们经常使用中间量来保存this的引用。成功的解决了this的问题,但是不够优雅。

function Person(name){
    this.name = name;
    this.say = function(){
        return ()=>{
            console.log(this.name);
        }       
    }
}
let person = new Person('tom');
person.say()();// tom

因为箭头函数没有绑定this,它的this是通过作用域寻找上一层的this,效果与上面的一样。

  • 由于不绑定this,因此使用call、apply、bind时第一个参数会被忽略。
    不使用箭头函数时,call正常修改this指向:
let obj = {
    base:1,
    addWithCall:function(x){
        let fn = function(){
            return x + this.base;
        }
        let b = {
            base:10
        };
        return fn.call(b,x); 
    }
}
console.log(obj.addWithCall(1));// 11

使用箭头函数,箭头函数内部的this是词法作用域,由上下文确定。也就是说this.base指向的是obj的base

let obj = {
    base:1,
    addWithCall:function(x){
        let fn = () => x + this.base;
        let b = {
            base:10
        };
        return fn.call(b,x); 
    }
}
console.log(obj.addWithCall(1));// 2
  • 不绑定arguments,获取的arguments是父作用域的。
// 例子1:
let arguments = [1,2,3];

let a = () => arguments[0];

console.log(a(9));// 1
// 例子2:
function b(){
    return () => {
        return arguments[0];
    }
}

console.log(b(1)(2));// 1

解决方案:使用剩余参数代替arguments

function b(){
    return (...args) => {
        return args[0];
    }
}

console.log(b(1)(2));// 2
  • 没有prototype属性
var Foo = () => {};
console.log(Foo.prototype); // undefined

相关文章

  • ES6箭头函数简介

    @(JS技巧)[ES6|箭头函数] ES6箭头函数(Arrow Functions) ES6可以使用“箭头”(=>...

  • es6全家桶(二)—— 箭头函数

    es6全家桶(二)—— rest参数、箭头函数 箭头函数 ES6 允许使用“箭头”(=>)定义函数。 var f ...

  • es6、js、css、jquery、vue以及程序设计 知识点总

    es6 列举常用的es6特性。 箭头函数this的指向。 eg:箭头函数的特性 箭头函数内部没有construc...

  • 箭头函数

    ES6允许使用“箭头”(==>)定义函数。 箭头函数 等同于

  • 学习 ES 6 箭头函数

    箭头函数的用法 ES6 允许使用“箭头”(=>)定义函数。 箭头函数的一个用处是简化回调函数。 箭头函数 this...

  • JavaScript箭头函数

    ES6新语法箭头函数 箭头函数是ES6新语法,因为语法简单、可读性好,所以使用的也很多。箭头函数也是匿名函数,区别...

  • 关于ES6箭头this的指向问题

    ES6 允许使用 “ 箭头 ” (=>)定义函数。 箭头函数 填 坑。 this的指向是 向上查找 非箭头函数的...

  • ES6箭头函数(Arrow Functions)

    箭头函数是什么?(What) 箭头函数 (Arrow Functions)也称“胖箭头函数”,是ES6全新的特性。...

  • 2019-01-11

    ES6 箭头函数 箭头函数表示法:()=>console.log('Hello') 箭头函数和普通函数的区别 和普...

  • js学习笔记4(函数)

    1.箭头函数 ES6新增属性。箭头函数特别适合嵌入函数的场景。 箭头函数虽然语法简介,但是很多场合不适用。箭头函数...

网友评论

      本文标题:ES6 - 箭头函数

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