The chained invocation pattern allows a method to call an object one after the other. This mode does not consider saving the return value of the function, so the entire invocation can be done in the same line:
Myobj.method1 ("Hello"). Method2 (). METHOD3 ("World"). METHOD4 ();
If there are methods in the object that do not require a return value, you can let it return the this reference, which makes it easy to continue calling the next method:
var obj = { value:1, increment:function () { this.value + = 1; return this; }, add:function (v) { This.value + = V; return this; }, shout:function () { alert (this.value); }};/ /Chain Method Callsobj.increment (). Add (3). Shout (); 5//as opposed to calling them one by Oneobj.increment (); Obj.add (3); Obj.shout (); 5
The benefit of chained invocation patterns is that the code reads like a normal sentence, and also encourages programmers to write a more single, modular approach; The disadvantage is that running multiple functions in a row is difficult to debug. However, it is always convenient to return a method that does not require a return value. Many JavaScript libraries, such as jquery, use this pattern extensively.
JavaScript Base Object creation mode chained invocation pattern (Chaining pattern) (029)