ES6箭头函数
2017-10-25 09:52
225 查看
ES6 中,箭头函数就是函数的一种简写形式,使用括号包裹参数,跟随一个
需要注意的是,上面栗子中的
当然,箭头函数不仅仅是让代码变得简洁,函数中
我们经常需要使用一个变量来保存
An arrow function does not have its own
=>,紧接着是函数体:
var getPrice = function() { return 4.55; }; // Implementation with Arrow Function var getPrice = () => 4.55;
需要注意的是,上面栗子中的
getPrice箭头函数采用了简洁函数体,它不需要
reture语句,下面这个栗子使用的是正常函数体:
var materials = [ 'Hydrogen', 'Helium', 'Lithium', 'Beryllium' ]; materials.map(function(material) { return material.length; }); // [8, 6, 7, 9] materials.map((material) => { return material.length; }); // [8, 6, 7, 9] materials.map(material => material.length); // [8, 6, 7, 9]
当然,箭头函数不仅仅是让代码变得简洁,函数中
this总是绑定总是指向对象自身。具体可以看看下面几个栗子:
我们经常需要使用一个变量来保存
this,然后在 growUp 函数中引用:
function Person() { var self = this; self.age = 0; setInterval(function growUp() { self.age++; }, 1000); }
var person = new Person();
An arrow function does not have its own
this;the
thisvalue of the enclosing execution context is used. Thus, in the following code, the
thiswithin the function that is passed to
setIntervalhas the same value as
thisin the enclosing function:
function Person(){ this.age = 0; setInterval(() => { // |this| 指向 person 对象 this.age++; }, 1000); } var person = new Person();
相关文章推荐
- ES6中箭头函数的知识点
- [js高手之路] es6系列教程 - 箭头函数详解
- es6 箭头函数(arrow function) 学习笔记
- [js高手之路] es6系列教程 - 箭头函数详解
- ES6 => 箭头函数
- ES6箭头函数(Arrow Functions)
- es6 箭头函数
- ES6中箭头函数的作用
- ES6语法---箭头函数/关于this指向
- es6中箭头函数学习的一个记录
- ES6的箭头函数介绍
- ES6的箭头函数
- ES6学习——箭头函数
- ES6箭头函数中的this绑定问题
- es6箭头函数的注意要点
- es6中箭头函数学习的一个记录
- ES6箭头函数和它的作用域
- ES6新特性(函数默认参数,箭头函数)
- ES6的箭头函数介绍
- ES6中新增的箭头函数