Home  >  Article  >  Web Front-end  >  How to implement chain call jQuery plug-in

How to implement chain call jQuery plug-in

anonymity
anonymityOriginal
2019-05-24 14:16:512718browse

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.

How to implement chain call jQuery plug-in

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn