扩展jQuery插件 [英] Extend a jQuery plugin

查看:101
本文介绍了扩展jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个jQuery插件,例如$ .abc = function(){}.现在,我想提供一个额外的模块,该模块使用与$ .abc相同的作用域并扩展其功能.例如在我的abc插件中,我将检查是否说出变量mod1 = true;如果正确,那么我将自动调用mod1的thoso函数.

Say I have a jQuery plugin like $.abc = function() {}. Now I want to provide an extra module which uses the same scope as $.abc and extends its functionality. For e.g. in my abc plugin I will check if say a variable mod1 = true; if its true, then I will automatically call thoso functions of mod1.

我的问题是我如何编写mod1,以便它可以与我的基本插件共享相同的名称空间,并且我可以从我的插件中调用它,例如mod1.getFiles();

My question is how do I write mod1 so that it can share the same namespace as my base plugin and I am able to call it from my plugin e.g. mod1.getFiles();

谢谢您的宝贵时间.很抱歉,如果不是很清楚.如果您有任何问题,我会再次解释.

Thankyou for your time. Sorry if this is not very clear. I will explain again if you have any questions.

更新:如何调用pluginbar的功能测试?

UPDATE: How do I call function test of pluginbar?

$.fn.pluginFoo = function() {
    ...
     CALL FUNCTION TEST OF PLUGINBAR
    ...
};

$.fn.pluginBar = function() {
    ...
    function test() {
        alert('this is a test'); 
    }
    ...
};

推荐答案

无需创建共享数据对象.您只需使用return语句就可以将一个插件的某些功能公开给其他插件.

There is no need to create a shared data object. You can expose some functionality of one plugin to other plugins using simply return statement.

$.fn.pluginFoo = function () {
     // here you can access public "members" of pluginBar
     $.fn.pluginBar.fn3();
     $.fn.pluginBar.fn4();
}


$.fn.pluginBar = function () {
    // private "members" (only this plugin can access them)
    var fn1 = function () { ... }
    var fn2 = function () { ... }

    // public "members" (other plugin can access that as well)
    // fn3 is defined in the return, fn4 allows to access private function fn1
    return {
        fn3: function () { ... },
        fn4: fn1
    };
}

这篇关于扩展jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆