• js self = this的解释


    Demo 1:

      function Person(){
            this.name = 'hjzgg';
            this.age = 24;
    
            this.show = function(){
                alert(name + " " + age);
            }
        }
    
        var p = new Person();
        p.show();

      错误:name 和 age都没有定义。

    Demo 2:

      function Person(){
            this.name = 'hjzgg';
            this.age = 24;
    
            this.show = function(){
                alert(this.name + " " + this.age);
            }
        }
    
        var p = new Person();
        p.show();

      正确。

    Demo 3:

      function Person(){
            this.name = 'hjzgg';
            this.age = 24;
    
            this.show = function(){
                alert(this.name + " " + this.age);
            }
        }
    
        var p = new Person();
        p.show.call({});

      错误:name 和 age 未定义。

    Demo 4:

      function Person(){
            this.name = 'hjzgg';
            this.age = 24;
    
            var self = this;
    
            this.show = function(){
                alert(self.name + " " + self.age);
            }
        }
    
        var p = new Person();
        p.show.call({});

      通过 var self = this,正确。

    Demo 5:

      function Person(){
            this.sayHello = function(){
                alert('hello world!');
            }
    
            this.show = function(){
                sayHello();
            }
        }
    
        var p = new Person();
        p.show();

      错误:sayHello未定义。

    Demo 6:

      function Person(){
            var sayHello = function(){
                alert('hello world!');
            }
    
            this.show = function(){
                sayHello();
            }
        }
    
        var p = new Person();
        p.show();

      正确。

      

    结论:

      每个函数都有属于自己的this对象,这个this对象是在运行时基于函数的执行环境绑定的,即在全局对象中,this指向的是window对象;在自定义函数中,this对象指向的是调用这个函数的对象,也就是说,this指向的是调用执行环境的那个对象。如果是在函数嵌套环境中,this指代的是调用外部函数或者内部函数的执行环境的对象。
      那么这里可能又会出现新的疑问:为什么self.name 和 self.age是正确的呢?
      其实这又涉及到另一个话题:实例成员与局部成员。我们创建构造函数的意义就是要用它来创建实例,那么所有属于实例的成员都需要用this来定义;而只有那些不属于实例的成员才不会用this定义;当然,用this定义了方法以后,在函数作用域内部要调用此方法时,就需要加上this了。

    Demo 7:

     var person = {
            name : 'hjzgg',
            age : 24,
            show : function(){
                alert(name + " " + age);
            }
    
       }
    
       person.show();

      错误:name 和 age未定义。

    Demo 8:

     var person = {
            name : 'hjzgg',
            age : 24,
            show : function(){
                alert(this.name + " " + this.age);
            }
    
       }
    
       person.show();

      正确。

    Demo 9:

     var person = {
            name : 'hjzgg',
            age : 24,
            show : function(){
                alert(this.name + " " + this.age);
            }
    
       }
    
       person.show.call({});

      错误:name 和 age 未定义。

    Demo 10:

    var person = {
            name : 'hjzgg',
            age : 24,
            show : function(){
                alert(person.name + " " + person.age);
            }
    
       }
    
       person.show.call({});

      正确。

  • 相关阅读:
    [转]难过的时候看看,也许会豁然开朗
    热爱生活
    [转]MTOM 编码
    11/16
    11/10 The Day Before Single's Day
    About working overtime
    hehe
    The First Blog
    配置MapServer出现的一些问题及解决办法
    Ubuntu 系统下终端快捷键设置
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/5330486.html
Copyright © 2020-2023  润新知