javascript - Is it possible to have an object method return a function object and if so how do you access it? -
for instance, if have:
var model = { updatecat: function(cat){ var foo1 = function(){ //do }; var foo2 = function(){ //do else }; return{foo1: foo1, foo2: foo2}; } }; model.updatecat.foo1(cat); //does not work what best way trying there? better create separate object methods?
you need parentheses after model.updatecat such model.updatecat().foo1(). because updatecat function needs called in order return foo1/foo2 object.
also, looks updatecat function takes parameter cat, not foo1 .foo1(cat) incorrect.
var model = { updatecat: function(cat){ var foo1 = function(){ //do }; var foo2 = function(){ //do else }; return{foo1: foo1, foo2: foo2}; } }; model.updatecat(cat).foo1(); //does work alternatively, can call updatecat function inline have updatecat reference result of function. note works if you're using 1 cat since result of calling updatecat(cat) stored , anonymous function lost.
var model = { updatecat: (function(cat){ var foo1 = function(){ //do }; var foo2 = function(){ //do else }; return{foo1: foo1, foo2: foo2}; })(cat) }; model.updatecat.foo1(); //does work
Comments
Post a Comment