4

I'm using the following format to avoid possible naming conflicts. My main aim is to keep the parts of the program in different files during development and then later combine it Editor is the

main.js

Editor=function(){
    this.manage=function(){

    }

}
var editor= new Editor;

a.js

Editor.prototype.A=function(){
        this.afunct=function(){

       }
}

b.js

Editor.prototype.B=function(){
      var this.var1;
      var this.var2;
      this.bfunct=function(){
           //call afunct() here
      }
}

A is a set of functions that does some testing,modification etc. afunct is a tester function which needs to be used in all the other files. B is supposed to act as a data package and new instances of it will be created to pass around.

How will I call afunct inside bfunct? Please help me understand how I can do this. Thank You in advance.

PS. I'm kind of a newbie in Javascript and please pardon any flaw in my logic.

3 Answers 3

1

It's obscure, but this might do it:

(function() {

    var Editor = function() {

    };

    Editor.prototype = {
        A: {
            afunct: function() {
                // Other functionality here.
            }
        },
        B: {
            bfunct: function() {
                Editor.prototype.A.afunct.call(this);
            }
        }
    };

    window.Editor = Editor;

})();


var editor = new Editor();

editor.B.bfunct();
Sign up to request clarification or add additional context in comments.

6 Comments

namespace. where you do variable = new something; etc etc..
@Eli i guess, but your in an infinite loop, the OP only wants to call A from B, not B from A
Ah, you are correct (good catch). I was just trying to demonstrate that you can call either method from the other. I will update again :)
Also, could you tell me why the function is made to execute ( wrapped in(function(){ } )()?
Instances of B needs to be created inside the editor, not outside it.
|
0

From inside B this should work

Editor.A.apply(this)

1 Comment

0

Try this from inside Editor.prototype.B:

Editor.prototype.B=function(){
      var this.var1;
      var this.var2;
      var self = this;
      this.bfunct=function(){
           //call afunct() here
           self.prototype.B.afunct();
      }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.