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?
-
No, whether a method is "chaining" or not doesn't have anything to do with whether or what prototype it is defined on.Bergi– Bergi2019年10月20日 22:21:27 +00:00Commented Oct 20, 2019 at 22:21
1 Answer 1
"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.
8 Comments
$(...).find(...).val().toString().split() chaining just like jQuery.