JavaScript deep nesting features

I cannot find a suitable example of the love of my life on how to do this or even if it is possible. Based on my general understanding of exmaples fragments, I came up with the following structure

         var t = function()
         {
             this.nestedOne = function()
             {
                 this.nest = function()
                 {
                     alert("here");
                 }
             }
         } 
         t.nestedOne.nest();

However, this does not work (obviously). I would really appreciate it if someone could point me in the right direction!

+5
source share
4 answers

This is done simply:

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

Your code otherwise makes no sense. thisan internal function does not apply to the function itself; it refers to the context of the object into which the function is called. And you do not even call functions in your code.

obj.func(), this func obj . this.asd = true "asd" true.

, -:

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

   }

   ClassA.prototype.method1 = function() {

   };

   function ClassB() {

   }

   ClassB.prototype.method1 = function() {

   };

   return ClassA;
}())

ClassA ClassB. , java.

+3

. http://jsfiddle.net/CstUH/

function t(){
     function f(){
         this.nest = function()
         {
             alert("here");
         }
     }
     this.nestedOne = new f();
 }
var myt=new t();
myt.nestedOne.nest()

1:

new t().nestedOne.nest()

var myt=new t();
myt.nestedOne.nest()

(http://jsfiddle.net/CstUH/1/)

2:

:

function t(){
    this.nestedOne = new function(){
        this.nest = function(){
            alert("here");
        }
    }
}
new t().nestedOne.nest()

http://jsfiddle.net/CstUH/2/

+2

JS- , [. ).

, t, t:

t();

t.nestedOne,nest(), t nestedOne - :

var t = {

    nestedOne : {

        nest : function()
        {

            alert("here");

        }        

    }

};

t.nestedOne.nest();                ​

John Resig Learning Advanced JavaScript, .

+1

, , . , , , .

    function test () {
      this.that = this;
      this.root = this;

      this.jCallback = new Array(new Array()); // 2d
      this.jCallbackCount = -1;
      this.str = "hello";


      // Callback handler... 
      this.command = {
        that : this, // let keep a reference to who above us on the food chain
        root : this.root, // takes us back to the main object

        // add : function() { var that = this; console.log(that.that.str); },
        add : function(targetFnc, newFunc) { 
            var that = this; 
            var home = that.that; // pretty much root but left in as an example of chain traversal.
            var root = this.root; // useful for climbing back up the function chain

            // console.log(that.that.str); 
            home.jCallbackCount++;
            // target, addon, active
            home.jCallback[home.jCallback.length] =  { 'targetFunc' : targetFnc,  'newFunc' : newFunc,  'active' : true, 'id': home.jCallbackCount};

            console.log('cbacklength: ' + home.jCallback.length);
            console.log('added callback targetFunction:[' + targetFnc + ']');

            return home.jCallbackCount; // if we want to delete this later...      
        },

        run : function(targetFnc) {
            var that = this; 
            var home = that.that;
            console.log('running callback check for: ' + targetFnc + '  There is : ' + (home.jCallbackCount + 1) + 'in queue.');
            console.log('length of callbacks is ' + home.jCallback.length);

            for(i=0;i < home.jCallback.length - 1;i++)
            {
              console.log('checking array for a matching callback [' + targetFnc + ']...');
              console.log('current item: ' + home.jCallback[i]['targetFunc'] );
              if( home.jCallback[i]['targetFunc'] == targetFnc )
              {
                 // matched! 
                home.jCallback[i]['newFunc']();
              }

                // console.log(that.that.jCallback[i].targetFunction);
            }
        }
      };

    }

    test.prototype = {
      say : function () {
        var that = this;
        console.log('inside');
        // that.command('doSay');
        that.command.run('doSay');
        console.log(that.str);
      }


    } // end proto



    // BEGIN TESTING **************************************************************************
    // BEGIN TESTING **************************************************************************
    // BEGIN TESTING **************************************************************************
    var testing = new test();


    testing.command.add('doSay', function () { console.log('213123123'); } );
    testing.command.add('doSay', function () { console.log('12sad31'); } );
    testing.command.add('doSay', function () { console.log('asdascccc'); } );


    testing.say();

: http://jsfiddle.net/Ps5Uf/

  • : , "".
0

All Articles