js---箭頭函式
阿新 • • 發佈:2018-11-26
箭頭函式的使用,我們在做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); };