js---箭頭函數


箭頭函數的使用,我們在做VUE或者angular項目的時候,定義的函數,很多時候會有作用域的問題,特別是在進行異步請求的時候,就必須使用到箭頭函數:

最簡單的箭頭函數:

function change((res)=>{
    console.log(res);
});
// 相當於
function change(res){
    console.log(res);
}; 

異步請求使用箭頭函數:

this.axiosPost(this.post.url,{},(res)=>{
    console.log(res);
});
axiosPost:function(url,data,fun){
    data['_token'] = _token;
    axios({
        method: 'post',
        url:url,
        data:Qs.stringify(data),
    }).then((res)=>{fun(res);});
}

單獨函數使用箭頭函數:

this.axiosPost(url,{},(res)=>{
    if(res.data.code == 200){
        praiseFun(res);
    };
});
var praiseFun = (res) =>{ 
    console.log(res);
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM