次のパターンを使用して jquery プラグインを作成しています。bar
関数が固有の値を返すようにしたいのですが、$(this)
代わりに常に返されるようです。
コードに何か問題がありますか?
(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)