Javascript Jquery/JS:在Jquery函数外调用Jquery函数的函数

Javascript Jquery/JS:在Jquery函数外调用Jquery函数的函数,javascript,jquery,function,Javascript,Jquery,Function,如何在包装此函数的函数之外调用此函数? 我想调用my_func(),就像在这个代码示例中一样。 (窗口加载功能只是一个示例。) 我想从“无处不在”调用my_func(),而不执行my_func()中的其他函数或代码。但是我想使用函数()的变量 使用my_func()我想更新存储在the_func()参数中的值。下面是一个示例,说明我通常如何编写插件,并将其应用于您的情况: 请注意,我是如何在可以调用的范围内创建一个泛型my_func函数的。methods中的my_func方法是通过插件语法向外

如何在包装此函数的函数之外调用此函数?
我想调用
my_func()
,就像在这个代码示例中一样。
(窗口加载功能只是一个示例。)
我想从“无处不在”调用
my_func()
,而不执行
my_func()
中的其他函数或代码。但是我想使用
函数()的变量


使用
my_func()
我想更新存储在
the_func()

参数中的值。下面是一个示例,说明我通常如何编写插件,并将其应用于您的情况:

请注意,我是如何在可以调用的范围内创建一个泛型
my_func
函数的。
methods
中的
my_func
方法是通过插件语法
向外界公开的。而
my_func()
my_func
函数是私有的,无法直接访问


调用不同方法的语法与大多数jQuery插件相同。

如何像调用
$(“div”)那样调用它。_func(“my_func”)
?这样行吗?我不是说用你目前的解决方案…我是说如果你对这个语法没意见的话,因为我知道这是一种很好的方法来构造它,让它在你想要的时候工作。但是如何在“init”-函数中调用“my_func”。。为什么要使用“返回此。每个(func…)”@johndesmith刚刚更新了我的答案。我使用
返回这个。每个
都可以在jQuery插件调用中进行链接。因此,您可以执行类似于
$(“div”).the_func(“method”).addClass(“something”).show()的操作
-在调用
函数
后,任何jQuery方法都会应用于原始选择器
(function($){
    $.fn.the_func = function() {

        function my_func(){
            alert('it works');
        }

        my_func();

        // other code

    };
})(jQuery);

$(window).load(function(){
    my_func(); // This way?
    $.the_func().my_func(); // Or this way? No?
    $.the_func.my_func(); // No?
    // ?
});

$(document).ready(function(){
    $('div').the_func();
});
(function ($) {
    function my_func(element) {
        console.log("it works: " + element.innerHTML);
    }

    var methods = {
        init: function (options) {
            console.log("from init");
            console.log("options for init: " + JSON.stringify(options));
            my_func(this);
        },

        my_func: function (options) {
            console.log("from my_func");
            console.log("options for my_func: " + JSON.stringify(options));
            my_func(this);
        }
    };

    $.fn.the_func = function (method) {
        var args = arguments;
        var argss = Array.prototype.slice.call(args, 1);

        return this.each(function () {
            if (methods[method]) {
                methods[method].apply(this, argss);
            }
            else if (typeof method === "object" || !method) {
                methods.init.apply(this, args);
            }
            else {
                $.error("Method " + method + " does not exist on jQuery.the_func");
            }
        });
    };
})(jQuery);

$(document).ready(function () {
    $("div").the_func({    // Same as passing "init" and { } as params
        test: "testing"
    });
});