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指向。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章