self vs this hoisting

by Himanshu Joshi

JavaScript

var myObject = {
  foo: "bar",
  func: function() {
    var self = this;
    console.log("outer func:  this.foo = " + this.foo); //myObject here
    console.log("outer func:  self.foo = " + self.foo); ////myObject here
    (function() {
      hoisted_foo = "yes";
      console.log("inner func:  this.foo = " + this.foo); //this is window =>undefined foo
      console.log("inner func:  inner foo = " + foo); // without window "this"
      console.log("inner func:   hoisted foo = " + hoisted_foo); // it is there!
      console.log("inner func:  self.foo = " + self.foo); //myObject here

      var foo = "inner foo"; //JavaScript Initializations are Not Hoisted
      var hoisted_foo; //declared and not defined =>this is hoisted.
    }());
  }
};
myObject.func();