Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  146] [ 7]  / answers: 1 / hits: 31012  / 13 Years ago, mon, september 19, 2011, 12:00:00

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


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

As far as I know chaining is one of the advantages against other famous frameworks. Can someone explain to me how chaining works here?


More From » jquery

 Answers
77

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/


[#90027] Saturday, September 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;