I am using the following pattern to write a jquery plugin, and I want the bar function to return intrinsic value, but it seems always to return $(this) instead.
Anything wrong with the code?
(function($){
var Foo = function(elements, options){
... ...
}
Foo.prototype = {
constructor: Foo,
bar: function(){
var value = ...
// do something here
return value;
},
}
$.fn.foo = function (option, kwargs){
return this.each(function(){
var $this = $(this),
data = $this.data('foo'),
options = typeof option == 'object' && option
if (!data) $this.data('foo', (data = new Foo(this, options)))
if (typeof option == 'string') return data[option](kwargs)
})
}
})(jQuery)
Nope, the code is correct. The problem is that it’s currently set up to always return a jQuery selector (the return value of
this.each). To return the result of your function instead, you could modify your function for$.fn.foolike so: