<script>
/*1.只有一个参数的时候*/
var print1=obj => console.log(obj);
print1("print1");//print1
/*2.有两个参数的时候,且只有一行代码,return可以省略*/
var print2=(a,b)=> a+b;
console.log(print2(2, 3));//5
/*3.当函数中有多行代码的时候,return不能省略*/
var print3=(c,d)=>{
console.log("c是"+c);//c是4
console.log("d是"+d);//d是5
return c+d;
}
console.log(print3(4,5));//9
/*4.在json对象中使用箭头函数*/
var name2="tangzhi";
let person={
"name":"xiaozhi",
eat1:function (foot = "鱼") {
//xiaozhi吃鱼
console.log(`${this.name}吃${foot}`);
},
//注意:箭头函数不能使用this
//xiaozhi吃meet
eat2:foot=>console.log(`${person.name}吃....${foot}`),
eat3(foot){
//xiaozhi吃虾
console.log(`${this.name}吃${foot}`);
}
};
person.eat1("鱼");
person.eat2("meet");
person.eat3("虾");
/*5.箭头函数加对象解构*/
var person2={
"name":"xiaozhi",
"age":25
};
//你好:xiaozhi
var print5=({name})=>console.log("你好:"+name);
print5(person2);
//6.扩展运算符(...)用于取出参数对象所有可遍历属性然后拷贝到当前对象
//1.深拷贝 Object { name: "TANGZHI", age: 25 }
let person1 = { name: "TANGZHI", age: 25 }
let someone = { ...person1 }
console.log(someone);
//2.合并对象
let age = { age: 15 };
let name = { name: "YANGXING" };
let person2 = { ...age, ...name }
//Object { age: 15, name: "YANGXING" }
console.log(person2);
</script>