forX
forX

Reputation: 2153

javascript namespace call other function methods

I try to change some way to call methods into namespace.

I split every namespace in files (want to keep it that way)

I try How to call function A from function B within the same namespace? but I didn't succed to split namespaces.

my fiddle sample got only 1 sub-namespace but could be more.

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();

Maybe I should change the my namespace methode? maybe prototype will do what I want?

Upvotes: 0

Views: 637

Answers (3)

pacifier21
pacifier21

Reputation: 813

"Objects" in javascript are not built the same way as in most object-oriented languages. Essentially, what you are building is a hierarchy of static methods that have no real internal state in-and-of themselves. Therefore, when one of the defined methods is invoked, the context (or state) of that method depends on what object invoked the method.

If you want to have any internal context, you will need to create an "instance" of an "object prototype". At that point, you can use "this.otherFunction" within your other functions. Here is a small example:

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();

You might get some more information about object definition here

Upvotes: 0

Saravana
Saravana

Reputation: 40604

You can capture the this in a variable because this inside $(function() {}) will point to document object. The below will work provided you never change the calling context of onReady -- i.e. it is always called on the Test1 object and not called on other context:

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

To access Print you have to reference using the Master object like: Master.Print() as it won't be available in the Test1 object

Upvotes: 1

guest271314
guest271314

Reputation: 1

this is document within .ready() or jQuery() alias for .ready() where function(){} is parameter $(function() {}). this at this.Function2() will reference document.

Upvotes: 0

Related Questions