2017-05-26 5 views
0

メソッドを名前空間に呼び出す方法を変更しようとしています。 (動作しない)this.MyFunction()の作成、親のメソッド(私はいけないその可能だと思う) javascript namespace他の関数メソッドを呼び出す

  • を呼び出すと、別の方法(主にjqueryのonReadyイベント関数)内の呼び出し
  • 継承関数を呼び出す

    • 私は、ファイル内のすべての名前空間を分割する(そのように維持したい)

      私はHow to call function A from function B within the same namespace?を試みるが、私は名前空間を分割するsuccedませんでした。

      私のフィドルサンプルはサブネームスペースが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(); 
      
      • IはMaster.Test1.Function1()を使用します。 Function1は同じ名前空間の中にあるので、私はそれを変更したいと思います。

      • 私はMaster.Print( "Function3")を使用します。私はそれを変えることはできないと思う。私がそれを使用しようとする方法は、より継承機能です。しかし、私はそれを行う方法があるかどうか知りませんか?

      多分私の名前空間の方法を変更する必要がありますか?多分プロトタイプが私の望むことをするだろうか?

      this
  • 答えて

    1

    $(function() {})内部thisdocumentオブジェクトを指していますので、あなたは変数にthisをキャプチャすることができます。

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

    あなたのようMasterオブジェクトを使用して参照する必要がPrintアクセスするには:常にTest1オブジェクト上と呼ばれ、他のコンテキストで呼び出されていない - すなわち、以下のあなたがonReadyの呼び出しコンテキストを変更することはありません提供動作します:Master.Print()Test1オブジェクトでは利用できません。

    +0

    クローゼットの道、タンクます:https://jsfiddle.net/forX/kv1w2rvc/1/ – forX

    0

    function(){}パラメータ$(function() {})ある.ready()ためdocument.ready()内又はjQuery()別名です。 thisthis.Function2())はdocumentとなります。

    0

    "オブジェクト"はほとんどのオブジェクト指向言語と同じように構築されていません。本質的に、あなたが構築しているのは、それ自身の真の内部状態を持たない静的メソッドの階層です。したがって、定義されたメソッドの1つが呼び出されると、そのメソッドのコンテキスト(または状態)は、そのメソッドを呼び出したオブジェクトによって異なります。

    内部コンテキストを使用する場合は、「オブジェクトプロトタイプ」の「インスタンス」を作成する必要があります。その時点で、他の関数内で "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

    関連する問題