第二章 ES6 箭头函数

箭头函数

使用方法

方法一、(必须得有括号)
	const  numbers=[1,2,5,7,21,132]
			const double=numbers.map(function(numbers){
				return numbers*2;
			});
			console.log(double);
			const double2=numbers.map((numbers,i)=>
				`${i}:${numbers*2}`;);
			console.log(double2);
方法二、
const double2=name=>{alert(name)};
double2('123')

箭头函数的this

--this 获取当前的值而不是其他作用域中的值或者是window里的值
const doubles={
		name:'jack',
		habbies:['food','sleep','ball'],
		printhabbies:function(){
		this.habbies.map(hobby=>{
			console.log(`${this.name} love ${hobby}`)	
					})
				}
			}
doubles.printhabbies();

ES6默认值

function multiply(a=5,b=3){
return a*b;
}
multiply();                        multiply(2);
multiply(undefined,3);       multiply(2,3);

箭头函数不适用场景

1 作为构造函数,一个方法需要绑定到对象

--错误
const Person=(name,age)=>{
	this.name=name;
	this.age=age;
}
--正确
const Person= function (name,age) {
	this.name=name;
	this.age=age;
}
const Persons=new Person('123','12');
const Persons=new Person('123','12');

2.真正需要this值
3.使用arguments

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值