访问jQuery插件的私有成员 [英] Access private members of jQuery plugin

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

问题描述

jQuery插件使用这样的模式来隐藏插件的私有函数:

jQuery plugins use a pattern like this to hide private functions of a plugin:

(function ($) {
    var a_private_function = function (opts) {
        opts.onStart();
    }

    $.fn.name_of_plugin = function (options) {
        a_private_function(opts);
    }
})(jQuery);

然后jQuery使这些fn函数可用:

jQuery then makes those fn functions available like this:

some_callback = function() {};

jQuery('selector').name_of_plugin( { onStart: some_callback } );

现在我要覆盖 a_private_function 。有没有什么方法可以在不修补实际的插件代码的情况下访问它?

Now I'd like to override a_private_function. Is there any way I can access it without patching the actual plugin code?

我想也许我可以通过使用调用者来访问私有函数的执行上下文但是没有work:

I thought maybe I could access the execution context of the private function by using caller but that did not work:

some_callback = function() {
    console.log(some_callback.caller.a_private_function); // -> undefined
};

jQuery('selector').name_of_plugin( { onStart: some_callback } );


推荐答案

正如我在这个答案,访问jQuery插件的私有成员的唯一方法是修改插件源本身。

As I learned in this answer, the only way to access the private members of a jQuery plugin are to modify the plugin source itself.

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

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