首页 > 其他 > 详细

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

时间:2020-01-07 22:18:15      阅读:72      评论:0      收藏:0      [点我收藏+]
//箭头函数:是一种定义函数的方式
//当我们需要把一个函数作为返回值传给一个函数时通常会用到箭头函数
//两个参数
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。”)

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

原文:https://www.cnblogs.com/LazyPet/p/12163649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!