Jquery 调用插件方法
如果遵循以下步骤,如何从公共方法调用私有方法,反之亦然 我通常在Jquery 调用插件方法,jquery,jquery-plugins,Jquery,Jquery Plugins,如果遵循以下步骤,如何从公共方法调用私有方法,反之亦然 我通常在init方法中声明私有方法,如: var methods = { init: function(options) { var settings = $.extend({ }, options); return this.each(function() { var $this = $(this); var data = $this
init
方法中声明私有方法,如:
var methods = {
init: function(options) {
var settings = $.extend({
}, options);
return this.each(function() {
var $this = $(this);
var data = $this.data('griffin-editor');
this.trimSpaceInSelection = function () {
//how do I call a public method here?
//to get the this context correct.
}
if (typeof data !== 'undefined') {
return this;
}
//the rest of the code.
这可能是不正确的做法 如果“this context correct”(此上下文正确)的意思是,您希望使用此集合调用某个公共方法,并将其设置为trimSpaceInSelection中包含的值,那么您可以这样做:
。。。。
this.trimSpaceInSelection=函数(){
methods.somePublicMethod.apply(this,arguments);//这将把传递给trimSpaceInSelection的所有参数传递给somePublicMethod
}
....
如果要将此内部公共方法设置为当前jQuery集合,则:
为什么它与插件指南有什么不同?这可能有助于我了解…我会边走边发现。这些可能会有帮助,工作起来很有魅力。谢谢我这样做是为了创建args数组:
var args=[];args[0]=actionName有更好的方法吗?我只是在调用methods.somePublicMethod.apply($this,arguments)之前声明了这一点您可以通过两种方式来执行:methods.somePublicMethod.call($this,firstArg,secondArg,thirdArg)
或方法。somePublicMethod.apply($this[firstArg,secondArg,thirdArg])代码>这有助于:
....
this.trimSpaceInSelection = function () {
methods.somePublicMethod.apply($this, arguments); // this will pass all arguments passed to trimSpaceInSelection to somePublicMethod
}
....