Home > Article > Web Front-end > How to implement chain call jQuery plug-in
How to implement chain calling jQuery plug-in: first create the object and call your own method; then add [return this] at the end of the method; finally fold the object back so that the object can continue to call the method , thus realizing chain operation.
The basic condition for realizing chaining is to create the instance object first and call its own method.
Chain calls are implemented in the form of return this. By adding return this to the method on the object and returning the object, the object can continue to call methods to achieve chain operations.
Obj().init().setFlag();
Decomposition:
obj = Obj(); obj.init(); obj.setFlag();
If you need chain processing, you only need to return the current instance object this inside the method, because returning this of the current instance can be accessed again My own prototype.
Obj.prototype = { init: function() { ... return this; }, setFlag: function() { ... return this; } }
Benefits of chain calls: saving code and making the code look more elegant.
The problem with chain calls: All object methods return the object itself, which means there is no return value, so this method may not be suitable in any environment.
The above is the detailed content of How to implement chain call jQuery plug-in. For more information, please follow other related articles on the PHP Chinese website!