Deep nesting functions in JavaScript

javascript

Solution

That is simply done with:

var t = {
    nestedOne: {
        nest: function() {
            alert('here');
        }
    }
};

Your code otherwise doesn't make sense. `this` inside function doesn't refer to the function itself, it refers to the object context that the function is invoked in. And you are not even invoking the functions in your code.

If I say `obj.func()` then `this` inside `func` will be `obj` for that call. So assigning `this.asd = true` will assign `true` to that object's `"asd"` property.

If you wanted to do a nested class, it looks very different:

ClassA = (function() {
   function ClassA() {

   }

   ClassA.prototype.method1 = function() {

   };

   function ClassB() {

   }

   ClassB.prototype.method1 = function() {

   };

   return ClassA;
}())

only ClassA can now make instances of ClassB. This should achieve same goals as nested classes in java.

Problem

I cannot find an proper example for the love of my life on how to do this or even if this is possible. Based on my pieced together understanding from fragments of exmaples, I have come up with the following structure ``` var t = function() { this.nestedOne = function() { this.nest = function() { alert("here"); } } } t.nestedOne.nest(); ``` However this is not working (obviously). I would greatly appreciate if someone could point me in the right direction!

Original source