5

我找不到一个合适的例子来说明我生命中的挚爱如何做到这一点,或者即使这是可能的。根据我对示例片段的拼凑理解,我提出了以下结构

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

但是,这不起作用(显然)。如果有人能指出我正确的方向,我将不胜感激!

4

4 回答 4

3

只需通过以下方式完成:

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

否则您的代码没有意义。thisinside function 不是指函数本身,它指的是调用函数的对象上下文。你甚至没有在代码中调用函数。

如果我说obj.func(),那么this里面func将是obj那个电话。因此分配this.asd = true将分配true给该对象的"asd"属性。

如果你想做一个嵌套类,它看起来很不一样:

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

   }

   ClassA.prototype.method1 = function() {

   };

   function ClassB() {

   }

   ClassB.prototype.method1 = function() {

   };

   return ClassA;
}())

现在只有 ClassA 可以创建 ClassB 的实例。这应该实现与 java 中的嵌套类相同的目标。

于 2012-08-26T17:56:38.817 回答
2

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/

于 2012-08-26T18:00:26.527 回答
1

在 JS 中,函数是主要的类对象,您可以直接在代码中访问它们[即不使用反射等]。

您放入tbody 的代码将在实际执行时执行t

t();

你写了t.nestedOne,nest(),但t没有nestedOne财产——你应该这样做:

var t = {

    nestedOne : {

        nest : function()
        {

            alert("here");

        }        

    }

};

t.nestedOne.nest();                ​

我建议你去看看John Resig 的 Learning Advanced JavaScript教程,这对我很有启发。

于 2012-08-26T18:01:29.297 回答
0

我今天写的一个简单的回调处理程序作为我如何进行深度嵌套的示例。如果在代码风格方面不是蜜蜂的膝盖,我深表歉意,它让我的概念更加清晰。

    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's keep a reference to who's 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/

  • 注意:要查看控制台输出,只需在 chrome 中打开检查器并单击“控制台”选项卡。
于 2012-11-02T05:10:43.427 回答