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對象,就是定義時所在的對象,而不是使用它時所在的對象
作者:授客
QQ:1033553122
全國軟體測試QQ交流群:7156436
Git位址:https://gitee.com/ishouke
友情提示:限于時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額随意,您的支援将是我繼續創作的源動力,打賞後如有任何疑問,請聯系我!!!
微信打賞
支付寶打賞 全國軟體測試交流QQ群
