美文网首页
this对象练习

this对象练习

作者: 心存美好 | 来源:发表于2021-11-02 09:29 被阅读0次

    练习一

    var myObject = {
                foo: "bar",
                fun: function () {
                    var self = this;
                    // AO{
                    //   this:myObject   默认指向window,但发现是隐式绑定,所以指 
     //向myObject,假设地址s001
     //   self:myObject   var self = this后,self也指向myObject   假设地址001}
                    console.log("outer fun: this.foo = " + this.foo);
                    console.log("outer fun: self.foo = " + self.foo);
    
                    (function () {
                        //AO{ //默认绑定 AO天生带this默认指向window
                        //this:window;
                        //}
                        console.log("inner fun : this.foo = " + this.foo);
                        //自己this指向window,、window上没有foo属性,获取对象身上没有的属性就是undefined;
                        console.log("outer fun: self.foo = " + self.foo);
                        //自己作用域上没有self,所以到父作用域找self:myObject 
                    })();
                }
            }
            myObject.fun();
    

    通过这个题目,总结一条规律, 每个函数内部的作用域都有自己的this, 如果你希望使用父作用域或者其他作用域中的this,需要提前吧this存起来

    练习二

     var hero = {
            _name: "wuwei",
            getName: function (){
              return this._name;
            }
          }
          var stoleName = hero.getName;
    
          console.log(stoleName());   //默认绑定:变量中存的是函数的内存地址,变量是函数另外一个名字,通过变量可以找到函数执行
          console.log(hero.getName());//隐式绑定
    

    练习三

    var length = 10;
        function fn() {
          console.log(this.length);
        }
    
        var obj = {
          length: 5,
          method: function (fn) {
            //obj.method(fn, 1) AO{
            //this:obj 默认为window 发现obj打点调用
            //arguments:实参列表{0:fn,1:1,length:2}
            //}
            fn();        //默认绑定
            arguments[0]();   //隐式绑定
          }
        }
        obj.method(fn, 1);
      
    //arguments是对象,类数组,不是真正的数组,不能使用arguments.push()数组方法。
    //字符串的包装类对象,也是个类数组,不能使用数组的一些方法 new String("abdckd")  
    

    练习四

        function a() {
          y = function () {
            x = 2;
          };
          return function () {
            var x = 3;
            y();
            console.log(this.x);
          }.bind(this)        //将这个函数的this强制的绑定到了外侧函数的this上,以后bb()函数不管怎么调用都是指向的外侧函数的this
        }
    
        var bb = a();
        bb()    //显示绑定,因为前面被强制绑定了
    

    练习五

            var n = 10;
              var obj = {
                n: 5,
                c: a()    //  c属性的值是a函数执行的结果,所以要先执行a函数
              };
              function a(){
                var n = 7;
                var c = function(){
                  var n = 3;
                  console.log(this.n);
                }
                return c;
              }
              obj.c(); // 5
             var aa = obj.c;
             aa()
    

    练习六

    var a = {},
          b = { name: "b" },
         
          c = { name: "c" };
         //对象的属性都是字符串,所以会将引用类型的对象转成字符串形式console.log(c.toString());为[object Object]
        a[b] = 123;    
        a[c] = 456;
        console.log(a);
        console.log(typeof(a));
    
        console.log(a[b]);
    

    练习七

    var a = {n:1};
          var b = a;
          a.n = a = {m:1}   //又创建了一个新对象,先赋值给a.n 再赋值给a,a.n的优先级高于a
          console.log(a);
          console.log(b)
    

    相关文章

      网友评论

          本文标题:this对象练习

          本文链接:https://www.haomeiwen.com/subject/nejxzltx.html