2017-05-26 94 views
0

我試圖改變某種方法來將方法調用到命名空間中。javascript命名空間調用其他函數方法

  • 調用父方法(我不認爲它有可能)
  • 創建和調用繼承功能
  • 內的另一個方法調用(主要是jQuery的onReady事件功能)(this.MyFunction()不工作)

我分裂文件中的每個命名空間(希望繼續保持這種方式)

我嘗試How to call function A from function B within the same namespace?但我沒有贏得成功分裂的命名空間。

我的小提琴只有1個子命名空間,但可能更多。

https://jsfiddle.net/forX/kv1w2rvc/

/************************************************************************** 
    // FILE Master.js 
    ***************************************************************************/ 
    if (!Master) var Master = {}; 
    Master.Print= function(text){ 
     console.log("master.Print :" + text); 
     $("body").append("<div>master.Print : " + text + "</div>"); 
    } 

    /************************************************************************** 
    // FILE Master.Test1.js 
    ***************************************************************************/ 
    if (!Master) var Master = {}; 
    if (!Master.Test1) Master.Test1 = {}; 
    /************************************************************************** 
    * Descrition : 
    * Function for managing event load/documentReady 
    **************************************************************************/ 
    Master.Test1.onReady = function() { 
     $(function() { 
      Master.Test1.Function1(); //try to replace because need all namespace.   

      try { 
        this.Function2(); //not working 
      } 
      catch(err) { 
       console.log("this.Function2 not working"); 
       $("body").append("<div>this.Function2 not working</div>"); 
      } 

      try { 
       this.Print("onReady"); //not working 
      } 
      catch(err) { 
       console.log("this.Print not working"); 
       $("body").append("<div>this.Print not working</div>"); 
      } 

      try { 
       Print("onReady"); //not working 
      } 
      catch(err) { 
       console.log("Print not working"); 
       $("body").append("<div>Print not working</div>"); 
      }  

     }); 
    } 

    Master.Test1.Function1 = function() { 
     console.log("Function1"); 
     $("body").append("<div>Function1</div>");  
     this.Function3();  //working because not inside another function 
    } 

    Master.Test1.Function2 = function() { 
      $("body").append("<div>Function2</div>"); 
     console.log("Function2"); 

    } 


    Master.Test1.Function3 = function() { 
      $("body").append("<div>Function3</div>"); 
     console.log("Function3"); 

     Master.Print("Function3"); //try to replace because need all namespace.   
    } 

    Master.Test1.onReady(); 
  • 我使用Master.Test1.Function1();我想改變它,因爲Function1在同一個命名空間內。

  • 我使用Master.Print(「Function3」);我不認爲我可以改變這一點。我嘗試使用它的方式,更多的是繼承功能。但我不知道是否有辦法做到這一點?

也許我應該改變我的命名空間方法?也許原型會做我想要的東西?

回答

1

您可以在一個變量中捕獲this,因爲this裏面的$(function() {})會指向document對象。下面將工作提供了你永遠不會改變的onReady調用上下文 - 即它總是叫Test1對象上,而不是呼籲其他方面:

Master.Test1.onReady = function() { 
    var self = this; 
    $(function() { 
     self.Function1(); 
     // .. 
    }); 
} 

要訪問Print您在使用Master對象喜歡引用:Master.Print(),因爲它不會在Test1對象中可用

+0

衣櫃辦法做到這一點,你坦克:https://jsfiddle.net/forX/kv1w2rvc/1/ – forX

0

thisdocument.ready()jQuery()別名.ready()其中function(){}是參數$(function() {})thisthis.Function2()將參考document

0

JavaScript中的「對象」的構建方式與大多數面向對象的語言不同。從本質上講,你所建立的是靜態方法的層次結構,它們本身並沒有真正的內部狀態。因此,當調用其中一個定義的方法時,該方法的上下文(或狀態)取決於調用該方法的對象。

如果你想有任何內部上下文,你需要創建一個「對象原型」的「實例」。此時,您可以在其他函數中使用「this.otherFunction」。這裏是一個小例子:

var MyObject = function() {}; 

MyObject.functionOne = function() { 
    console.log("Function 1"); 
    this.functionTwo(); 
}; 

MyObject.functionTwo = function() { 
    console.log("Function 2"); 
}; 

var instanceOne = new MyObject(); 
instanceOne.functionOne(); 

你可能會得到關於對象的定義一些更多的信息here

+0

大多數情況下,函數都是靜態的(不需要創建實例)。但它是一種做法。不幸的是,子功能的問題仍然存在。薩拉瓦納的答案對我的問題更有用。 – forX