假设我有一个像这样创建的jQuery插件(来自jquery站点):
(function ( $ ) {
$.fn.greenify = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
color: "#556b2f",
backgroundColor: "white"
}, options );
// Greenify the collection based on the settings variable.
return this.css({
color: settings.color,
backgroundColor: settings.backgroundColor
});
};
}( jQuery ));但是现在我可能需要一些更大的函数来帮助我的插件操作,哪里是放置这些函数并保持代码整洁、可读性和可测试性的最佳位置?
例如,我认为本地化函数不是一个好主意,如下所示:
(function ( $ ) {
$.fn.greenify = function( options ) {
// This would be bad becase too many of these and my code will be clutered.
var aHelperFunctionThatIsHuge = function () {
// A lot of code here...
};
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
color: "#556b2f",
backgroundColor: "white"
}, options );
// Greenify the collection based on the settings variable.
return this.css({
color: settings.color,
backgroundColor: settings.backgroundColor
});
};
}( jQuery ));这根本不管用:
(function ( $ ) {
$.fn.greenify = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
color: "#556b2f",
backgroundColor: "white"
}, options );
// Greenify the collection based on the settings variable.
return this.css({
color: settings.color,
backgroundColor: settings.backgroundColor
});
};
// ideal but doesnt work
$.fn.greenify.aHelperFunctionThatIsHuge = function () {
// A lot of code here...
};
}( jQuery ));那我该把它们放哪儿呢?
谢谢
发布于 2014-02-04 00:46:50
在插件功能之上,在生命周期内:
(function ( $ ) {
var aHelperFunctionThatIsHuge = function () {
// A lot of code here...
};
$.fn.greenify = function( options ) {
};
}( jQuery ));https://stackoverflow.com/questions/21540875
复制相似问题