0

I noticed different behavior regarding this in public inner functions and private inner functions.

(function () {
    "use strict";

    function MyObject(){
        var myThis = this;
        var z = function(){
            console.log("Item",myThis);     
        };
        var y = function(){
           console.log("Item",this);
        };
        this.x = function(){
            console.log("Item",this);
        };
        this.out = function(){
            y();
            this.x();
        };
    }
    var myObject = new MyObject();
    myObject.out();
}());

The output is

"Item"
[object Object] {
  out: function (){
"use strict";

      z();
      y();
      this.x();
    },
  x: function (){
"use strict";

      window.runnerWindow.proxyConsole.log("Item",this);
    }
}
"Item"
undefined
"Item"
[object Object] {
  out: function (){
"use strict";

      z();
      y();
      this.x();
    },
  x: function (){
"use strict";

      window.runnerWindow.proxyConsole.log("Item",this);
    }
}

Why are both this different?

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62

0 Answers0