微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vue学习笔记es6中的箭头函数

//箭头函数:是一种定义函数的方式
//当我们需要把一个函数作为返回值传给一个函数时通常会用到箭头函数
//两个参数
const sum = (num1, num2) => {
        return num1 + num2;
    }
    //一个参数的时候是可以省略括号的
const fun = (num) => {
    return num * num
}
const fun2 = num => {
        return num * num
    }
    //函数中的代码情况2
    //多行代码
const test = () => {
        console.log('da')
        console.log('da')
    }
    //一行代码
const test2 = () => {
        console.log('da')
    }
    //省略大括号
const test3 = () => console.log('da')
    //箭头函数中this的使用
let fun1 = setTimeout(() => {
    console.log(this) //window
}, 1000);
let fun2 = setTimeout(function() {
    console.log(this) //window
}, 1000);
const obj = {
        bbb() {
            setTimeout(function() {
                console.log(this) //window
            }, 1000)
        },

        aaa() {
            setTimeout(() => {
                console.log(this) //obj
            }, 1000)
        }
    }
    //箭头函数中的this引用的是向外层作用域中一层层查找this直到有this
    //而在普通函数函数执行时,实际是window调用了它,也就是window.函数名();那么,里面的this指向当前调用函数的对象,就是window。”)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐