Vue ES6箭頭函式使用總結
by:授客 QQ:1033553122
箭頭函式
ES6允許使用“箭頭”(=>)定義函式:
函式不帶引數
定義方法:函式名稱 = () => 函式體
let func = () => 1
等同于
function func() {
return 1;
}
函式只帶一個引數
定義方法:
函式名稱 = 引數 => 函式體
或者
函式名稱 = (引數) => 函式體
let func = state => state.count
等同于
function func(state) {
return state.count;
}
函式帶多個引數
定義方法:函式名稱 = (引數1,引數2,...,引數N) =>函式體
let arg2 = 1
let func = (state, arg2) => state.count + arg2
等同于
function func(state,arg2) {
return state.count + arg2;
}
函式體包含多條陳述句
let author = {
name: "授客",
age: 30,
viewName: () => {
console.log("author name"); // 輸出undefined
// 當前this指向了定義時所在的物件
console.log(this.name); // 輸出undefined,并沒有得到"授客"
}
};
author.viewName();
注意
函式體內的this物件,就是定義時所在的物件,而不是使用它時所在的物件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/162721.html
標籤:JavaScript
上一篇:太極圖繪制
下一篇:VUE專案Eslint報錯
