js中箭头函数和普通函数的区别是什么

区别:

箭头函数是“=>”,普通函数是“function”。

箭头函数不能作为构造函数,不能使用new。

箭头函数不绑定arguments,但普通函数可以。

箭头函数中的this代表上层对象,普通函数中的this代表当前对象。

 

 

举例:

new Vue({
    el:'#root',
    //data的第一种写法:对象式
    /* data:{
        name:'尚硅谷'
    } */

    //data的第二种写法:函数式  data:function() {} 简写成 data() {}
    data(){
        console.log('@@@',this) //此处的this是Vue实例对象
        return{
            name:'尚硅谷'
        }
    }
})

 

不能写成箭头函数

new Vue({
    el:'#root',
    //data的第一种写法:对象式
    /* data:{
        name:'尚硅谷'
    } */

    //data的第二种写法:函数式  data:function() {} 简写成 data() {}
    data:()=> {
        console.log('@@@',this) //此处的this是Windows实例对象
        return{
            name:'尚硅谷'
        }
    }
})

 

 

 

 

 

 

 

 

https://www.php.cn/faq/471334.html

posted @ 2024-01-22 20:47  喻聪  阅读(645)  评论(0)    收藏  举报