【问题标题】:How to properly interact with parent functions/objects inside an object如何与对象内的父函数/对象正确交互
【发布时间】:2012-02-11 20:22:11
【问题描述】:

我有一个名为Application 的“主”对象,它将存储与此特定脚本相关的所有函数。 该对象中有一些不同的函数,例如start()pause(),它们与子对象交互。

当从子对象(应用程序对象,甚至更深的)调用这些函数时,我必须直接引用Application.function()。这会变得非常杂乱。如果我需要与子数据this.Game.instance.sessionId 进行交互,这些函数中的情况也是如此。它失败了,如果我将来随着需求的增长添加更多对象怎么办?只是为了与另一个子/父对象交互,它会变得非常混乱,更不用说冗长了。

示例代码:

    var Application = {     
       //Start the whole application
       start: function() {
          doSomething(this.Game.instance) //do something with the game instance object
       },

       pause: function() {
          //pause the current sessionId
          interactWithMyServer(this.Game.instance.sessionId); //clutty
       }

       Game: {  
          //redraw the game to reflect changes
          redraw: function() {
             someDrawFunction(this.instance); //draw the instance
          },

          //Stores information about the game instance from the server, changes often
          //bad example with the pause, but just to get the idea of my example
          instance: {
             gameId: 23,
             sessionId: 32,
             map: 32,

             //dummy function
             pause: function() {
             Application.pause(); //works, but I have to start with the "root" object, Application - how to avoid this?
             }
          }

      }             
   };

请原谅愚蠢的代码,只是想显示我的问题。

如何以最正确干净的方式构建这个,或者说重建?

【问题讨论】:

    标签: javascript object coding-style


    【解决方案1】:

    碰巧以您描述的方式定义的对象之间没有固有的永久关系。换句话说,为属性“Game”定义的对象与“Application”对象没有内在关联,“instance”也与“Game”没有内在关联。如果你想要它,你必须明确地给它一个与之相关的属性。

      var Application = {
        // ...
        Game: {
          //redraw the game to reflect changes
          redraw: function() {
             someDrawFunction(this.instance); //draw the instance
          },
    
          //Stores information about the game instance from the server, changes often
          //bad example with the pause, but just to get the idea of my example
          instance: {
             gameId: 23,
             sessionId: 32,
             map: 32,
             app: null,
    
             //dummy function
             pause: function() {
               this.app.pause(); //works, but I have to start with the "root" object, Application - how to avoid this?
             }
          }
    
    // ...
    
    Application.Game.instance.app = Application;
    

    【讨论】:

      【解决方案2】:

      您可以通过定义一些闭包方法来传递对父级的引用:

      var App= {
      
      
          aplicationFunction: function() {
              alert("Hello, yes this is application...");
          },
      
          app: this,
      
          getGameObj: function() {
              var _that = this;
              return {
      
                  that: _that,
      
                  parentF: function() {
                      this.that.aplicationFunction();
                  },
              };
          },
      };
      
      App.getGameObj().parentF();
      

      现场演示:http://jsfiddle.net/vZDr2/

      为了更舒适,您可以将其用作以下示例:

      gameobj = App.getGameObj();
      gameobj.parentF();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-02
        • 2012-02-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-25
        相关资源
        最近更新 更多