jquery链接如何工作? [英] how does jquery chaining work?

查看:108
本文介绍了jquery链接如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是问什么是链接的适当语法,我知道它可能是这样的:

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!

推荐答案

返回一个包含方法的对象,你可以简单地从返回的对象中调用一个方法。

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/

这篇关于jquery链接如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆