2017-05-26 97 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