問題描述
我不是在問什么是適當(dāng)?shù)逆溄诱Z法,我知道它可能是這樣的:
I am not asking what is the appropriate syntax for chaining, I know it could be something like:
$('myDiv').removeClass('off').addClass('on');
但是我真的很想了解它的內(nèi)部工作原理,據(jù)我所知,鏈接是相對于其他著名框架的優(yōu)勢之一,但是對于像我這樣的新手程序員來說,這對于我們來說非常抽象,我敢肯定有沒有人可以為我提供解釋,讓我了解鏈接的工作原理.
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.
謝謝!
推薦答案
如果你有一個帶有某些方法的對象,如果每個方法返回一個帶有方法的對象,你可以簡單地從返回的對象中調(diào)用一個方法.
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();
演示: http://jsfiddle.net/5kkCh/
這篇關(guān)于jquery 鏈接是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!