Я создаю свой собственный плагин jQuery. Вот код, который я написал до сих пор:
(function ($) {
$.fn.customPlugin.defaults = {
x: 'test',
y: 'foo'
};
$.fn.customPlugin = function (options) {
var opt = $.extend({}, $.fn.customPlugin.defaults, options);
return this.each(function () {
var current = $(this);
//I want to add instance methods
});
};
})(jQuery);
Далее я хочу добавить методы экземпляра в этот плагин. Теперь у меня есть два подхода к этому
1
(function ($) {
$.fn.customPlugin.defaults = {
x: 'test',
y: 'foo'
};
$.fn.customPlugin = function (options) {
var opt = $.extend({}, $.fn.customPlugin.defaults, options);
this.each(function () {
var current = $(this);
function method1() {
//opt will be used here
}
function method2() {
//opt will be used here
}
});
};
})(jQuery);
2
(function ($) {
$.fn.customPlugin.defaults = {
x: 'test',
y: 'foo'
};
$.fn.customPlugin = function (options) {
var opt = $.extend({}, $.fn.customPlugin.defaults, options);
this.each(function () {
var current = $(this);
$.fn.customPlugin.method1(opt);
$.fn.customPlugin.method2(opt);
});
};
$.fn.customPlugin.method1(opt)
{
//opt will be used here
};
$.fn.customPlugin.method2(opt)
{
//opt will be used here
};
})(jQuery);
Не могли бы вы указать мне, какой подход я должен использовать, или если вы можете предложить мне лучший подход, чем этот?