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);
};

猜你喜欢

转载自www.cnblogs.com/e0yu/p/10019496.html