Q:

Why isn't it possible to dynamically create a variable via self-invoking function in Javascript?

I'm trying to imitate some sort of constructor like in other programming languages.
If I do it like this it doesn't work. :/ Sorry for being dumb! :/
Thanks for the help!!
  function foo(){

      this.makeVar = function(){this.newVar = 'hello world'}(); 

  }

 var test = new foo();
 alert(test.newVar); 

A:

Because you are calling the (anonymous) function directly, and not as a method on an object. So this is window.
Copy the value of this in the outside function to a variable that is still available on the inside function.
function foo(){
  var self = this;
  this.makeVar = function(){
    self.newVar = 'hello world';
  }(); 
}

