February 27, 2012

Use multiple methods

Question by consindo

I’m having trouble using multiple methods in JavaScript. What would be the best way to construct something like this:

task.data(id).display();
task.data(id).edit();

Answer by Starx

Its possible but little tricky

function data(id) {
  return {
    display: function () {
      alert('some message');
    }
  };
}
//now use it
data(id).display();

Demo


For this to work in your context, you need to chain three methods

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!