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