how to call function inside object literal function

How do I call this.test inside this javascript structure?

var app = {
  init: function(){
      this.cache();
      this.bind();
  },
  cache: function(){
      this.test = function(){
          console.log("this");
      }
      console.log( "cache."  );
  },
  bind: function(){
      console.log( "bind."  );
  },
  doSomething: function(){
      console.log( "Simple app."  );
  }
}

app.init();

Open in new window

Al4ddin2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
As far as I know, you can't. It's defined as local to that function ("app.cache"). Either define it elsewhere or assign it to a variable, or as the method of some other object, that's accessible from a wider scope. Here's an example of the latter:
  cache: function(){
      app.test = this.test = function(){
          console.log("this");
      }
      console.log( "cache."  );
  },

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve BinkCommented:
I'm not sure what your question is.  See the modified example and it's output, done in Chrome's console:
>var app = {
  init: function(){
      this.cache();
      this.bind();
  },
  cache: function(){
          this.test = function(){
          console.log("this");
      }
      console.log( "cache."  );
  },
  bind: function(){
      this.test();   //<---------- See what i did there?
      console.log( "bind."  );
  },
  doSomething: function(){
      console.log( "Simple app."  );
  }
}

>app.init();
cache.
this
bind
>app.test()
this

Open in new window

The interior function should be available as long as:
app.cache() has been run at least once,
you are defining it with the `this.function_name = function() {}` syntax
By available, I mean it should be recognized external to the object using app.test, or from within the object by calling this.test.
0
RobOwner (Aidellio)Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Kravimir's comment #a40968327

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Steve BinkCommented:
My solution demonstrated that Kravimir's assertion of "it can't be done" was incorrect.  My solution actually used the OP's original code, with a minor change for demonstrative purposes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.