调用插件方法
如果我遵循插件创作指南,如何从公共方法调用私有方法,反之亦然?
我通常在 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.
这可能是错误的做法?
How do I invoke a private method from a public one and vice versa if I follow the plugin authoring guide?
I usually declare the private methods within the init
method like:
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.
It might be the incorrect thing to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“这个上下文正确”意味着你想要调用一些公共方法,并将其设置为在trimSpaceInSelection内部的值,那么你可以这样做:
如果你想将这个内部公共方法设置为当前的jQuery集合,那么:
If by 'this context correct' you mean that you want call some public method with this set to value which this has inside trimSpaceInSelection then you can do it like this:
And if you want set this inside public method to current jQuery collection then: