我正在使用一个样板插件设计,看起来像这样,
;(function ( $, window, document, undefined ) {
    var pluginName = "test",
        defaults = {};
    function test( element, options ) {
        this.init();
    }
    test.prototype = {   
        init: function() {}
    }
    $.fn.test = function(opt) {
        // slice arguments to leave only arguments after function name
        var args = Array.prototype.slice.call(arguments, 1);
        return this.each(function() {
            var item = $(this), instance = item.data('test');
            if(!instance) {
                // create plugin instance and save it in data
                item.data('test', new test(this, opt));
            } else {
                // if instance already created call method
                if(typeof opt === 'string') {
                    instance[opt].apply(instance, args);
                }
            }
        });
    };
})( jQuery, window, document );
现在说我有两个<div>同班container。
现在我会test像这样在这些 div 上调用我的插件,
$(".container").test({
    onSomething: function(){
    }
});
现在,当onSomething从我的插件内部调用函数时,我如何调用引用实例onSomething函数的插件公共方法?
例如,第一个 div 发生了一些事情,并且 container只为第一个onSomethingdiv调用了函数。 container
为了让它更清楚一点,我试图将this实例传递给onSomething函数,这样我就可以公开所有插件数据,然后我可以做类似的事情,
onSomething(instance){
   instance.someMethod();
   instance.init();
   //or anything i want
}
对我来说,这看起来很错误,所以必须有更好的方法......或者没有?