I'm kind of newbie on JS, but I've done my research on the subject and found no exhaustive answer.
I'm using Angular JS for a project, and I have a service that expose some functions:
var app = angular.module('myApp', [])
app.service('mySrv', function() {
this.myFoo = function() {
var anObject = {}
//some code on the object...
return anObject
}
this.myGoo = function() { // function(object) maybe?
//some code on an object again
return aNewObject
}
}
Then in my controller I want to do something like JQuery does, chaining the output of myFoo as the input of myGoo
$scope.myObj = mySrv.myFoo(['some','params']).myGoo('another_one')
I've found that I could use the prototype properties in order to add chainability, as stated here How to make chainable function in JavaScript? But the same answer says that it should not be done on Objects, which will be my case...
Last but not least, ther is this other method JavaScript Object Method Chaining: useful?
which, frankly, I didn't understand...
Can someone clear all this fog from my mind?
EDIT: Using and mixing the great examples from @plalx and @Connor, I've managed to get this snippet working, and as you can see, is missing the "chainable part" of @plalx example.
What the hell that part exactly does?
var o = {
setTest: function (test) {
this.test = test;
return this
},
getTest: function () {
this.test = this.test.toUpperCase()
return this
},
splitMe: function() {
return this.test.split('')
}
};
console.log( o.setTest('hello').getTest().splitMe() )