简单理解this指向

this的指向

其实this的指向很简单,记住一句话就行

在哪里调用就返回哪里(箭头函数除外)
下面是一些例子

*箭头函数网上说它没有指向,箭头函数指向的是上一级this所指向的


<script> 
        //  1. 函数调用模式  this ---> window   函数名()
        //  2. 方法调用模式  this --->调用方法的对象  对象.方法名()  obj ={say:function(){console.log(this)}}
        //  3. 构造函数调用模式 this ---> 创建出来的实例 new 函数名()
        //  4. 在元素事件中this----->指向当前节点对象
        //  5. 箭头函数中this没有明确指向,会向上一级(宿主对象)寻找
        
    
    var obj1 = {
        name: '小番茄',
        say() {
            console.log(this);
            function fn3() {
                console.log(this, 'fn3'); // window
                    // let fn5 = () => {
                    // console.log(this, 'fn5');  //obj
                    // }
                    // fn5();
                }
            // let fn3=function(){
            //     console.log(this);    //window
            // }                          
                // 箭头函数的this指向宿主对象
            let fn4 = () => {
                console.log(this, 'fn4');  //obj
            }
        fn3();
        fn4();
        // f(){
        //     console.log(this);//obj
        // }
        // d:()=>{
        //     console.log(this);  //window
        // }
      }  
    }
    // obj1.f()
    // obj1.d();
    obj1.say();
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值