1、对象的简化写法
let name='兔子';
let dance=function(){
console.log("我喜欢跳舞");
}
const rabbit={
name,change,
//方法声明简化
improve1:function(){
console.log("我的舞蹈技能提高了");
}
improve2(){
console.log("我的舞蹈技能提高了");
}
}
console.log(school);
2、箭头函数
函数声明
let fun =function(a,b){
//方法体
}
let fun=(a,b)=>{
return a+b;
//方法体
}
//调用函数
let result=fn(1,2);
console.log(result);
1、箭头函数的this是静态的,始终指向函数声明时所在作用域下的this的值
function getName(){
console.log(this.name);
}
let getName2=()=>{
console.log(this.name);
}
window.name='兔子';
const school={
nane:'rabbit'
//直接调用
getName();
getName2();
//两者都输出兔子
//call方法调用,指向别的值
getName.call(school);//普通方式的改变输出rabbit;
getName2.call(school);//依旧输出兔子
2、不能作为构造实例化对象
//这样写是错的
let Person=(name,age)=>{
this.name=name;
this.age=age;
}
let me=new Person('xiao',30);
console.log(me);
3、不能使用arguments变量
//这样写是错的
let fun=()=>{
console.log(arguments);
}
fun(1,2,3);
4、可以省略形参
//(1)当形参有且只有一个的时候,可以省略小括号
let add=n=>{
return n+n;
}
console.log(add(9));
//2、当代码体只有一条语句时,可以省略花括号,此时return必须省略
//语句的执行结果就是函数的返回值
let pow=n=> n*n;
console.log(pow(9));