gpt4 book ai didi

javascript - 在 jquery 插件模式中从公共(public)函数调用私有(private)函数

转载 作者:行者123 更新时间:2023-11-30 17:24:54 26 4
gpt4 key购买 nike

我是新手,所以请耐心等待,

我有以下类型的插件模式

;(function ( $, window, document, undefined ) {
'use strict';
defaults = {..}
// The actual plugin constructor
function Plugin ( element, options ) {
this.init();
}
Plugin.prototype = {
init: function () {
....
},
privateFunc: function ( options) {
//do private stuff
}
};

$.fn.plugin = function (method, options ) {
if (method === undefined){
method = 'null';
}
switch(method){
case 'null':
return this.each(function() {
if ( !$.data( this, "plugin_custom" ) ) {
$.data( this, "plugin_custom", new Plugin( this, options ) );
}
});
case 'publicFunc':
// do stuff with options and then call privateFunc with some data
break;
}
};
})( jQuery, window, document );

我想从 publicFunc 调用 privateFunc 但我不确定如何访问它

最佳答案

Plugin.prototype.privateFunc 实际上根本不是私有(private)的;作为原型(prototype)的一部分意味着公众可见度;这意味着您可以像这样访问它:

var plugin = $.data( this, "plugin_custom");
plugin.privateFunc();

关于javascript - 在 jquery 插件模式中从公共(public)函数调用私有(private)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com