开发者

jQuery : return this

In jQuery plugins, every time we return an object of jQuery, like

$.fn.Myplugin = function() {
      return this.each(function() { //do some stuff });
});

My doubt is, why do we actually return an object of jQuery and where are we going to use this retur开发者_开发知识库ned object

Though I don't return any jQuery object in my function(plugin), I still get the same result just as in returning an object

someone please explain my doubt,

Thanks for sparing your time, Have a Good Day !


The jQuery object is returned so we can do method chaining:

$('#somelement').doSomething().doAnotherThing().doOneMoreThing();

If you don't return it from one of your plugin methods, there's no more jQuery object to work with so subsequent calls will result in an error.


All jQuery methods return you jQuery Object so that the set of results returned can further be used to perform other operations this is also called chaining. This is done for tersness in the code. Its easy to code (atleast for me) and prevents me writing big loops.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜