ES6需要关注的变化

对象的方法简写,可以省略function:

let obj ={
    userName:'鸣人',
    age:10,
    getUserName(){
        return this.userName;
    },
    getAge:function(){
        return this.age;
    }
}
console.log(obj.getUserName());
console.log(obj.getAge());

 

箭头函数

//没有参数,写空括号
let fn = () => {
    console.log('hello');
};

fn();

如果箭头函数有一个参数,也可以省去包裹参数的括号

//只有一个参数,可以省去参数括号
let fn2 = name => {
    console.log(`hello ${name}!`)
};

fn2("tom");

有多个参数

let sayHi =(name,age)=>{

    console.log(`名称为:${name},年龄为${age}`);
}

sayHi('tom',3);

 

箭头函数没有原型prototype,因此箭头函数没有this指向。

 

posted on 2023-08-07 15:35  荆棘人  阅读(4)  评论(0编辑  收藏  举报

导航