1

I feel really dumb asking this question, but I can't find a decent answer. I have an object of functions like so.

var methods = {
    init : function(){},
    one : function(){},
    two : function(){}
};

Let's say in function one I wanted to access function two. I know that I could do methods.two() and access it that way, but is there a a different way to do it, so I'm not referencing it from the outside, because that will get confusing with other parts of my code quickly.

1
  • this.two() should work Commented Oct 6, 2015 at 22:29

1 Answer 1

2

Use this operator as a reference to the object instance:

var methods = {
    init : function(){ console.log('In INIT'); },
    one : function(){ console.log('In ONE');
                      this.two();
                    },
    two : function(){ console.log('In TWO');}
};

methods.one();

Sign up to request clarification or add additional context in comments.

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.