0

I would like to structure my app like this:

APP.open = (function ($) {
  return {
    people: function() {
      return {

        gender: function() {},

        age: function() {}
      }
    }
  };
}(jQuery));

I can get APP.open.people(), but when I do: APP.open.people.gender(); I get undefined.

4
  • 2
    APP.open.people().gender(); Commented Apr 21, 2015 at 17:26
  • Isn't APP.open a function? Just asking, because I'm not really an expert on javascript. Commented Apr 21, 2015 at 17:27
  • 1
    @Nolonar - APP.open is an object, as the function you see is an IIFE that executes immediately and returns an object. APP.open.people on the other hand, is not an IIFE, and has to be called. Commented Apr 21, 2015 at 17:28
  • @adeneo. I see. Looks like I know what I'll be researching tonight, thanks ;) Commented Apr 21, 2015 at 17:29

2 Answers 2

1

You can do something like this to achieve what you are asking: Note the anonymous function wrapping around the people assignment.

APP.open = (function ($) {
return {
  people: (function() {
    return {

      gender: function() {},

      age: function() {}
     }
   })()
 };
}(jQuery));
Sign up to request clarification or add additional context in comments.

4 Comments

this looks interesting. Just one question. How can I extent the people method from another file?
so what i would like to do is to define APP.open.people.race() method from a different file.
To do that you can use object literal. App.open = { people: { gender: function(){ ... } } } and from another file you can add methods to people App.open.people.race = function(){ ... }
Currently implementation would also work with App.open.people.race = function(){ ... } but object literal version would be more readable.
0

APP.open.people().gender();

silly, I over saw this... Thanks guys!

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.