[英]jQuery Plugin - Calling a function return Uncaught TypeError
I am not sure how to call the function in jQuery Plugin. 我不确定如何在jQuery插件中调用该函数。 I am trying to make something small like social sharer, it would be my first plugin but I am confused. 我正在尝试制作社交共享器之类的小东西,这将是我的第一个插件,但我感到困惑。
Uncaught TypeError: Object [object Object] has no method '_echoCookie'
Here's what it does... 这就是它的作用...
function EchoSoc(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
After that we have some stuff into init: function () { }
such as: 之后,我们将一些东西放入init: function () { }
例如:
$('body').append( //-->
'<div id="fb-root" />'
+ '<script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";fjs.parentNode.insertBefore(js, fjs);}(document, "script", "facebook-jssdk"));</script>'
+ '<script src="//platform.twitter.com/widgets.js"></script>'
+ '<script src="//apis.google.com/js/plusone.js"></script>'
// <--
);
and as this is a test for google as sample here's one last piece: 由于这是对Google的测试,下面是最后一个示例:
$('.echoGoogle').append('<g:plusone size="medium" href="' + this.options.google_url + '" callback="googleCB"></g:plusone>');
Then we have these functions: 然后我们有以下功能:
_echoEvents: function () {
googleCB = function() {
this._echoCookie();
};
},
_echoCookie: function () {
$.cookie('echoSoc', 'done', { expires: 30, path: '/' });
console.log('cookie added');
}
But this simply won't work... 但这根本行不通...
_echoEvents: function () {
googleCB = function() {
this._echoCookie();
};
}
Well my question is how to call the function within other functions below init... this._functionName(); 好吧,我的问题是如何在init下面的其他函数中调用该函数... this._functionName(); only works into init not in the functions below it. 仅可用于init而不在其下面的函数中。 Thanks in advance. 提前致谢。
It should be as given below since, when you call _echoCookie
with in _echoEvents
the this
variable might be pointing to a different context. 它应如下所示,因为当您在_echoEvents
调用_echoCookie
时, this
变量可能指向不同的上下文。 So use a closure variable to hold the reference to the main object and use it inside googleCB
因此,请使用闭包变量保存对主对象的引用,并在googleCB
使用它
_echoEvents: function () {
var that = this;
googleCB = function() {
that._echoCookie();
};
},
_echoCookie: function () {
$.cookie('echoSoc', 'done', { expires: 30, path: '/' });
console.log('cookie added');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.