首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JQuery插件的实用程序函数放在哪里?

将JQuery插件的实用程序函数放在哪里?
EN

Stack Overflow用户
提问于 2014-02-04 00:44:35
回答 1查看 58关注 0票数 0

假设我有一个像这样创建的jQuery插件(来自jquery站点):

代码语言:javascript
复制
(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 ));

但是现在我可能需要一些更大的函数来帮助我的插件操作,哪里是放置这些函数并保持代码整洁、可读性和可测试性的最佳位置?

例如,我认为本地化函数不是一个好主意,如下所示:

代码语言:javascript
复制
(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 ));

这根本不管用:

代码语言:javascript
复制
(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 ));

那我该把它们放哪儿呢?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 00:46:50

在插件功能之上,在生命周期内:

代码语言:javascript
复制
(function ( $ ) {

    var aHelperFunctionThatIsHuge = function () {
      // A lot of code here...
    };

    $.fn.greenify = function( options ) {

    };

}( jQuery ));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21540875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档