箭头函数详讲(箭头函数中的this)

箭头函数详讲(箭头函数中的this)

箭头函数的基本使用:

在这里插入图片描述
在这里插入图片描述

知识点:

箭头函数是不能提升的,所以需要在使用之前定义。

使用 const 比使用 var 更安全,因为函数表达式始终是一个常量。

如果函数部分只是一个语句,则可以省略 return 关键字和大括号 {},这样做是一个比较好的习惯

箭头函数this的几种解释说法

1.当我们使用箭头函数的时候,箭头函数会默认帮我们绑定外层 this 的值,所以在箭头函数中 this 的值和外层的 this 是一样的。

2.箭头函数中的this引用的就是最近作用域中的this

3.向外层作用域中,一层一层查找this,直到有this的定义

4.有的箭头函数都没有自己的 this。 不适合定义一个 对象的方法

代码解释(注释超详细)

一、箭头函数的基本使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 定义函数的方式

        // js中的
        // 函数声明
        function bbb(){
            console.log(6);
        }
        // 函数表达式
        const aaa = function(){
            console.log(5);
        }


        // 视频中的
        // 1.function
        const a = function(){
            console.log(4);
        }

        // 2.对象字面量中定义函数
        const obj ={
            bbb: function (){
                console.log(1);
            },//这两种等效
            bbb (){
                console.log(2);
            }
        }

        // 3.ES6中的箭头函数
        const ccc = (参数列表) => {}

    </script>
</body>
</html>

二、箭头函数的参数和返回值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 参数问题
        // 1.1 放入两个参数
        const sum = (num1,num2) => {
            return num1*num2
        }
        // 1.2 放入一个参数
        const power = (num) => {
            num*num
        }//或  只有一个参数时小括号可以省去
        const power = num => {
            num*num
        }
        // 2.函数中
        // 2.1 函数代码块中只有一行代码
        const test = () => {
            console.log('Hello World');
            console.log('Hello Vue');
        }
        // 2.2 函数代码块只有一行代码
        const nul = (num1,num2) => num1+num2 //等效于(默认给出返回值return)
        const nul = (num1,num2) => {return num1+num2}

        // 无返回值时
        const demo = () => {
            console.log('Hello Demo');
        }//等效于
        const demo = () => console.log('Hello Demo');//(会执行代码输出结果)
        console.log(demo());//undefined

    </script>
</body>
</html>

三、箭头函数中this的使用

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        // 什么时候使用箭头函数
        setTimeout(function () {
            console.log(this);//window
        }, 1000)

        setTimeout(() => {
            console.log(this);//window
        }, 1000)

        // 结论:箭头函数中的this引用的就是最近作用域中的this
        const obj = {
            aaa() {
               setTimeout(function(){
                   console.log(this);//window
               }) 
               setTimeout(() => {
                   console.log(this);//obj
               })
            }  
        }
        const obj = {
            aaa() {
               setTimeout(function(){
                  setTimeout(function(){
                    console.log(this);//window
                  })

                // this
                  setTimeout(() => {//向外找,找到了42行作用域中的this,42行指向window
                      console.log(this);//window
                  })
               }) 

               setTimeout(() => {
                setTimeout(function(){
                    console.log(this);//window
                  })

                //   this(是47行箭头函数的this,继续像外层找,指向obj)
                  setTimeout(() => {//向外层找,找到47行作用域中的this
                      console.log(this);//obj
                  })
               })
            }  
        }
        // const obj = {
        //     aaa (){
        //         setTimeout(function () {
        //             setTimeout(function () {
        //                 console.log(this);//window
        //             })
        //         })

        //         setTimeout(() => {
        //             console.log(this);//window
        //         })

        //         setTimeout(() => {
        //             setTimeout(function(){
        //                 console.log(this);//
        //             })
        //         })

        //         setTimeout(() => {
        //             console.log(this);
        //         })
        //     }
            
        // }
        // aaa()
    </script>
</body>

</html>

注意:需要先看懂一、二才能更好的三哦理解,也可以帮助理解vue-compiler和vue-only中main.js的区别
在这里插入图片描述

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱代码的猿猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值