how does jquery chaining work?

2018-12-31 20:38发布

I am not asking what is the appropriate syntax for chaining, I know it could be something like:

$('myDiv').removeClass('off').addClass('on');

However I'm really curious to understand the inner working of it, as far as I know chaining is one of the advantages against other famous frameworks but this us to much abstraction for a novice programer like me, I'm sure there is someone out there that can provide me with a explanation that allows me to understand how chaining works.

Thanks!

9条回答
一个人的天荒地老
2楼-- · 2018-12-31 20:44

If you have an object with certain methods, if each method returns an object with methods, you can simply call a method from the object returned.

var obj = {   // every method returns obj---------v
    first: function() { alert('first');   return obj; },
    second: function() { alert('second'); return obj; },
    third: function() { alert('third');   return obj; }
}

obj.first().second().third();

DEMO: http://jsfiddle.net/5kkCh/

查看更多
美炸的是我
3楼-- · 2018-12-31 20:47

The point is that a function must evaluate to the "parent" function. So e.g.

foo().bar().test();

has to evaluate to:

foo().test();

so that you can call another function on foo(). To do this, you can return this:

function foo() {
    // empty, nothing interesting here
}

foo.prototype.bar = function() {
    return this;
}

foo.prototype.test = function() {
    return this;
}

Then,

var something = new foo();
something.bar() === something; // true

And because of this:

something.bar().test() === something.test(); // true

So because something.bar() evaluates to something, you can immediately call the second function in one go.

查看更多
美炸的是我
4楼-- · 2018-12-31 20:47

One of the way to chaining, check demo .

test("element").f1().f2().f3()
查看更多
柔情千种
5楼-- · 2018-12-31 20:48
return $this;

each jQuery function returns an instance of the jQuery class, which can then have methods called on it. you could break it down, and this code would have the same effect.

jQuery_obj = $('myDiv');
jQuery_obj = jQuery_obj.removeClass('off');
jQuery_obj = jQuery_obj.addClass('on');
查看更多
还给你的自由
6楼-- · 2018-12-31 20:54

Here is an example of conditional callback chaining, like is used on the $.ajax jQuery function.

// conditional callback function example
myFunction = function () {

    // define event callback prototypes without function parameter
    var callback_f = new Object;
    callback_f.callback1 = function () { return callback_f; };
    callback_f.callback2 = function () { return callback_f; };

    if ([condition]){
        // redefine the callback with function parameter 
        // so it will run the user code passed in
        callback_f.ReturnPlayer = function (f) { f(); return callback_f; };
    }else{ 
        callback_f.NewPlayer = function (f) { f(); return callback_f; };
    }

    return callback_f;
}
查看更多
路过你的时光
7楼-- · 2018-12-31 21:02

In chaining parent function/method returns an object which is then used by the child function/method, and things go on such a way. In short the jQuery or $ returns itself (an object) which allows the chaining.

It is the same mechanism below

var obj=$('input');    //returns jQuery object
var obj1=obj.val('a'); //returns jQuery object
var obj2=obj1.fadeOut();//returns jQuery object

It looks like this if it is done with chaining

$('input').val('a').fadeOut();
查看更多
登录 后发表回答