Reputation: 3103
I'm creating functions which are being chaining like jQuery with prototype like this:
String.prototype.rem = function (toRemove){
return this.replace(toRemove,'');
}
var str = 'aaaasaaaa';
console.log(str.rem('s'))
That's only an example.
My question is that, is it true way to define chaining function. Or am I doing it wrong?
Upvotes: 0
Views: 50
Reputation: 134
"Chaining" is related to "function call" and "return type".
"prototype" is related to common(or default) properties which live in constructor function.( factory of object instance )
not all "normal" method can change to chaining method. Method which return A value can not be chaining method. Method which have no return statement can be chaining method.
Check one of your method has async operation which may chain other sync method. Check error handling policy. It handled by you or user who use your library.
before design or implement chaining, Read about Type "Maybe" "Either" (Functional world!!!) It help you.
Upvotes: 1