箭头函数的好处;对象的函数属性简写,箭头函数与解构表达式的应用
箭头函数:可以不用编写基础函数的解构(如:function),直接使用 =>
var print = function (obj) {
console.log(obj);
};
print("print");
//箭头函数
var print2 = obj => console.log(obj);
print2("print2");
var sum = function (a, b) {
return a+b;
};
console.log(sum(1,2));
//箭头函数
var sum2 = (a,b) => a+b;
console.log(sum2(1,2));
var sum3 = (a,b) => {
console.log(a+b);
return a+b;
};
console.log(sum3(1,2));
let person = {
"name": "dada",
learn1: function (course="java") {
console.log(this.name + " 在学习 " + course);
},
//箭头函数;不能使用this
learn2: (course) => console.log(person.name + " 在学习 " + course),
//简写
learn3(course){
console.log(this.name + " 在学习 " + course);
}
};
person.learn1("java");
person.learn2("js");
person.learn3("css");
const person = {
name:"dada",
age:13,
language:["java","js","css"]
};
function hello(person) {
console.log("hello " + person.name);
}
hello(person);
//箭头函数、解构表达式
var hello2 = ({name}) => console.log("hello2 " + name);
hello2(person);