变量提升、作用域this实战(真题:看代码输出)

 1. 原型的应用

//
         function A() {
            this.name = 'smyhvae';
        }
        A.prototype.test = function () {
            setTimeout(function () {
                console.log('this.name');
            }, 1)
        }
        var a = new A();
        a.test(); // this.name

//
       this.a = 20
       function Go () {
          console.log(this.a)  // 40
          this.a = 30
        }
        Go.prototype.a = 40
        console.log(new Go().a)  // 30

//
        window.name = 'ByteDance';
        function A() {
            this.name = 123;
        }
        A.prototype.getA = function() {
            return this.name + 1;  
        };
        let a = new A();
        let funcA = a.getA;
        console.log(funcA());  //  ByteDance1
        console.log(a.getA()); //124
//
    Function.prototype.a = ()=>
        console.log(1)
    Object.prototype.b = ()=>
    console.log(2);
    function A(){}
    const a = new A();
    
    a.b();  // 2
    // a.a();  //报错

2. 函数局部作用域在函数结束后销毁

//
        var name = '全局'
        function getName() {
            var name = '局部';
            return this.name;
        };
        console.log(getName())  // undefined;  如果第一句是 name = '全局' 则输出全局



//
        let val = 1;
        function foo() {
            console.log(val);
        }
        function bar() {
            let val = 2;
            foo();
        }
        bar();   // 1
//
        let a={b:110}
        function aaa(o){
            o.b=120
            o={b:119}
        }
        console.log(aaa(a))  // undefined
        console.log(a)  // {b: 120}

 3. 注意对象的引用

//
     var obj ={
          a:4
        }
     function test(obj) {
         obj.a =3
        }
     test(obj)
     console.log(obj.a)  // 3   


//
        var length = 10
        function fn() {
            return this.length + 1
        };
        var obj = {
            length: 5,
            test1() {
                return fn()
            }
        }
        console.log(obj.test1())   // 11
        obj.test2 = fn
        console.log(obj.test1() === obj.test2())   // false
        let obj = {
            a: 0
        };

        function test(obj) {
            obj.a = 1;
            obj = {
                a: 2

            };
            obj.b = 3;
            console.log(obj) // {a: 2, b: 3}
        }
        test(obj);
        console.log(obj); //  {a: 1}
        let obj = {
            a: 0,
        };

        function test(obj) {
            obj = {
                a: 2,
            };
            obj.a = 1;
            obj.b = 3;
            console.log(obj);  //{a: 1, b: 3}
        }
        test(obj);
        console.log(obj); //{a: 0}

4. 箭头函数.call无效

//        
        const school = {
            name: "大哥",
        }
        function getName() {
            console.log("getName:" + this.name); 
        }

        getName1 = () => console.log("getName1:" + this.name);  
        window.name = "张三";

        getName();  // getName:张三
        getName1();   // getName1:张三
        getName.call(school);  // getName:大哥
        getName1.call(school);    // getName1:张三

//
        const obj = {
            birth: 1990,
            getAge(year) {
                let fn = y => y - this.birth;
                return fn.call({
                    birth: 2000
                }, year);
            },
        };
        console.log(obj.getAge(2020));  //30
        var a = 1;
        var obj = {
            a: 2
        };

        function fun() {
            var a = 3;
            let f = () => console.log(this.a);
            f();
        };
        fun();//1
        fun.call(obj);//2

5.函数作为对象键值

        var id = 'GLOBAL';
        var obj = {
            id: 'OBJ',
            a: function() {
                console.log(this.id);
            },
            b: () => {
                console.log(this.id);
            }
        };
        obj.a(); // 'OBJ'
        obj.b(); // 'GLOBAL'
        new obj.a() // undefined
        new obj.b() // Uncaught TypeError: obj.b is not a constructor

6. 非strict model的情况下:函数内部用var声明的变量为显示全局变量,最高提升到当前(函数)作用域;不用var声明的变量为隐式全局变量, 会提升到window。(strict model的情况:都不会提升到window)

window本身就是一个内置的对象 ,访问一个内置的对象无论什么属性,有值就显示值没有值就显示undefined

        let obj = {
            a: 0
        };
        function test(obj) {
            obj = {
                a: 3
            };
            var obj1 = {
                a: 3
            };
            obj2 = {
                a: 3
            };
            let obj3 = {
                a: 3
            };
        }
        test(obj);

        console.log(obj); // a: 0
        console.log(window.obj); // undefined
        // console.log(obj1);  
        console.log(window.obj1); //undefined
        console.log(obj2); //a: 3
        console.log(window.obj2); //a: 3
        // console.log(obj3);
        console.log(window.obj3); //undefined
   
      const name = 'list'
      function test() {
        console.log(this.name) // ''
      }
      let Obj = {
        name: 'aaa',
        people: () => {
          console.log(this.name) // ''
          test()
        },
      }
      Obj.people()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白目

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

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

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

打赏作者

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

抵扣说明:

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

余额充值