Ryan  Gittens
Ryan Gittens

Reputation: 15

Is it possible to have an object method return a function object and if so how do you access it?

For instance, if i have:

var model = {
    updateCat: function(cat){
         var foo1 = function(){
                 //do something
             };
         var foo2 = function(){
                 //do something else
             };
         return{foo1: foo1, foo2: foo2};
    }
};

model.updateCat.foo1(cat); //does not work

What is the best way to do what i am trying to do there? Would it be better just to create separate object methods?

Upvotes: 1

Views: 48

Answers (3)

Roko C. Buljan
Roko C. Buljan

Reputation: 206344

Another interesting way that allows you to chain your methods:

var model = {
    updateCat : {
        foo1 : function( cat ) {
           // do tha thing
           return this;
        },
        foo2 : function( cat ) {
           // do things
           return this;
        }
    }
};

model.updateCat.foo1(cat);           //does work
model.updateCat.foo1(cat).foo2(cat); //does work

In your example, updateCat is a function that is not executed, so you don't execute it (), instead you try to use it like it's a object ({}) updateCat.foo1 therefore you might get an error saying

Expected an assignment or function call and instead saw an expression

for your model.updateCat.anythingHereIsUnreachable( bla );

Upvotes: 2

arcyqwerty
arcyqwerty

Reputation: 10695

You need parentheses after model.updateCat such that it is model.updateCat().foo1(). This is because updateCat itself is a function which needs to be called in order to return the foo1/foo2 object.

Also, it looks like the updateCat function takes parameter cat, not foo1 so .foo1(cat) would be incorrect.

var model = {
    updateCat: function(cat){
         var foo1 = function(){
                 //do something
             };
         var foo2 = function(){
                 //do something else
             };
         return{foo1: foo1, foo2: foo2};
    }
};

model.updateCat(cat).foo1(); //does work

Alternatively, you can call the updateCat function inline to have updateCat be a reference to the result of that function. Note that this only works if you're only using one cat since the result of calling updateCat(cat) is stored and the anonymous function is lost.

var model = {
    updateCat: (function(cat){
         var foo1 = function(){
                 //do something
             };
         var foo2 = function(){
                 //do something else
             };
         return{foo1: foo1, foo2: foo2};
    })(cat)
};

model.updateCat.foo1(); //does work

Upvotes: 0

Edwin Reynoso
Edwin Reynoso

Reputation: 1541

If you want to change it into an object:

(function() {
  var foo1 = function() {}
  var foo2 = function() {}
  var model = {
     updateCat: {foo1: foo1, foo2: foo2}
  }
  window.model = model;
})();

Or if you want to use return keyword:

var model = (function() {
  var foo1 = function() {}
  var foo2 = function() {}
  var model = {
     updateCat: {foo1: foo1, foo2: foo2}
  }
  return model;
  // also return like this:
  // return { updateCat: {foo1: foo1, foo2: foo2};
})();

Now you can do:

model.updateCat.foo1(cat);

Upvotes: 0

Related Questions