0

How can I access the function from other namespace?

Eg:

FavoriteFlavor.instructState.prototype = { 
    test: function() {
      // call function show from chooseEmoticonState
      // I have tried calling FavoriteFlavor.chooseEmoticonState.show();
    }
}

FavoriteFlavor.chooseEmoticonState.prototype = {
    show: function() {
    }
}
2
  • do you have to code with prototype or are you allow to use a different pattern of javascript?? Commented Jan 8, 2015 at 6:39
  • 1
    Since prototypes are meant to be methods that are given to newly created objects (not to be used as namespaces), whenever someone is regularly looking to call methods on the prototype directly rather than call them on an instantiated object, it makes me wonder if your functions are laid out properly and should be in an actual namespace and not on a prototype. Commented Jan 8, 2015 at 6:41

3 Answers 3

2

One way would be to call the method directly from the prototype object:

FavoriteFlavor.chooseEmoticonState.prototype.show();

However, any this instance references would not point to an instance of instructState. You can correct this by using the call or apply method, and passing this as the first parameter.

FavoriteFlavor.chooseEmoticonState.prototype.show.call(this);

Also, unless you have a reason not to, you could simple add the show method of the chooseEmoticonState prototype to the instructState prototype.

FavoriteFlavor.instructState.prototype.show = FavoriteFlavor.chooseEmoticonState.prototype.show;
Sign up to request clarification or add additional context in comments.

Comments

1

The show function is on FavoriteFlavor.chooseEmoticonState.prototype, not FavoriteFlavor.chooseEmoticonState, so you could call it with:

FavoriteFlavor.chooseEmoticonState.prototype.show();

But given that it's on a prototype, it doesn't really make a lot of sense to call it directly.

What would make sense is to call it on an instance of chooseEmoticonState:

// assume chooseState has been instantiated with 
//    new FavoriteFlavor.chooseEmoticonState();
chooseState.show();

Or if show() isn't intended to be an instance method, put it directly on FavoriteFlavor.chooseEmoticonState:

FavoriteFlavor.chooseEmoticonState = {
    show: function() {
    }
};

and then you can call it in the fashion you were trying to before:

FavoriteFlavor.choseEmoticonState.show();

1 Comment

you are right! Thanks you for explaining. Can you please simplified about prototype pattern? I'm a bit confusing about that.
1

in a different javascript pattern: (just for the sake of diversity)

http://jsfiddle.net/za6muccw/

FavoriteFlavor = {};

FavoriteFlavor.chooseEmoticonState = (function(){
         //private
    var myprivate = "im private";

    function show(){
        alert("goo")
    }

    return {
        show : show //its making me public
    }
})();

FavoriteFlavor.instructState = (function(){
   //private
   var myprivate = "im private";
   FavoriteFlavor.chooseEmoticonState.show();
})();

2 Comments

Still trying to work out the point of a private variable that isn't accessed by anything. The second IIFE doesn't return anything, so the value of FavoriteFlavor.instructState will be undefined.
its just to make an example of private variable,this pattern name is anonymous function, which allow to create public and private variable, in the second "object class" there is not any return since you are not publishing nothing and in the first one you are returning show which its a reference to the function show() you are making it public, now FavoriteFlavor.chooseEmoticonState.show its public

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.