首頁 文章 箭头函数

箭头函数

2019-07-10 17:51  瀏覽數:1711  來源:小键人27611    

const double1 = function(number){
return number * 2;
}
const double2 = (number) => {
return number * 2;
}
const double3 = (number) => number * 2;
const double4 = number => number * 2;
const double5 = (number => number * 2);
const double6 = (number,number2) => number + number2;
const double = (number,number2) => {
sum = number + number2
return sum;
}
// console.log(double(20,20));
// map一个数组,让数组中的值以double形式展现
const numbers = [1,2,3];
var newNumbers = numbers.map(number => number * 2);
// console.log(newNumbers);
// 改变this指向
const team1 = {
members:["Henry","Elyse"],
teamName:"es6",
teamSummary:function(){
let self = this;
return this.members.map(function(member){
// this不知道该指向谁了
return `${member}隶属于${self.teamName}小组`;
})
}
}
const team2 = {
members:["Henry","Elyse"],
teamName:"es6",
teamSummary:function(){
return this.members.map(function(member){
// this不知道该指向谁了
return `${member}隶属于${this.teamName}小组`;
}.bind(this))
}
}
const team = {
members:["Henry","Elyse"],
teamName:"es6",
teamSummary:function(){
return this.members.map((member) => {
// this指向的就是team对象
return `${member}隶属于${this.teamName}小组`;
})
}
}
console.log(team.teamSummary());



聲明:以上文章均為用戶自行添加,僅供打字交流使用,不代表本站觀點,本站不承擔任何法律責任,特此聲明!如果有侵犯到您的權利,請及時聯系我們刪除。

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)