Reputation: 5094
I am learning Jquery and I am trying to get how to make a plugin.
$.fn.myfun = function(options) {
if (typeof options === 'string') {
callOption.apply(this, arguments);
} else {
initElements.call(this, options);
}
return this;
};
I am not able to understand why there is a statement return this ? Why do we return it?
Upvotes: 0
Views: 34
Reputation: 101513
This is so you or the end user of your plugin can chain other jQuery methods after calling your plugin. For example:
$('.foo').myFun().slideToggle();
Returning this
returns a jQuery object in this case, maintaining access to all the methods jQuery has.
Upvotes: 4