[英]How can i call a public method from within a private one when using the javascript Module Pattern?
I would like to call a public method from a private one but the property "this" refers to the window object. 我想从私有方法调用公共方法,但属性“this”指向窗口对象。
Please note i am trying to apply the module pattern. 请注意我正在尝试应用模块模式。 You can find a working code example at jsfiddle.net
您可以在jsfiddle.net上找到一个有效的代码示例
// how can i access a public method from a private one?
// (in this example publicAlert from privateMethod)
// this refers to the window object.
$(function() {
var modulePattern = (function($)
{
var privateMethod = function()
{
appendText("called privateMethod()");
this.publicAlert();
};
var appendText = function(texToAppend)
{
var text = $('#output').text() + " | " + texToAppend;
$('#output').text(text);
};
return {
publicMethod : function()
{
appendText("called publicMethod()");
privateMethod();
},
publicAlert : function()
{
alert("publicAlert");
}
};
});
mp = new modulePattern($);
mp.publicMethod();
});
If you want to be able to do that you need to declare the 'public' function like you would a private function, and then expose it as public. 如果您希望能够这样做,您需要像私有函数一样声明'public'函数,然后将其公开为public。 Like this:
像这样:
$(function() {
var modulePattern = (function($) {
var privateMethod = function() {
appendText("called privateMethod()");
publicAlert();
};
var appendText = function(text) {
var text2 = $('#output').text() + " | " + text;
$('#output').text(text2);
};
var publicAlert = function(){
alert("publicAlert");
};
return {
publicMethod: function() {
appendText("called publicMethod()");
privateMethod();
},
publicAlert: publicAlert
};
});
mp = new modulePattern($);
mp.publicMethod();
});
[Edit] I would also encourage you to get into the habit of clicking on the 'jslint' button at the top of jsfiddle, your code was missing a few semicolons, and you also redeclared the 'text' variable inside your appendText function (it was already passed in) [编辑]我也鼓励你养成点击jsfiddle顶部的'jslint'按钮的习惯,你的代码缺少几个分号,你还重新声明了你的appendText函数中的'text'变量(它已经通过了)
Also, you're using the module pattern in a slightly different way to how I've learned it. 此外,您使用的模块模式与我学习它的方式略有不同。 Do you have a link to your reference material?
您是否有指向参考资料的链接?
This is how I would have done the module pattern as I know it: http://jsfiddle.net/sVxvz/ [/Edit] 这就是我所知道的模块模式: http : //jsfiddle.net/sVxvz/ [/ Edit]
Also, if you use the module pattern correctly, you can refer to the public functions by using the module name, like this: 此外,如果正确使用模块模式,则可以使用模块名称来引用公共函数,如下所示:
var testModule = (function($) {
var privateMethod = function() {
appendText("called privateMethod()");
testModule.publicAlert();
};
var appendText = function(text) {
var text2 = $('#output').text() + " | " + text;
$('#output').text(text2);
};
return {
publicMethod: function() {
appendText("called publicMethod()");
privateMethod();
},
publicAlert: function() {
alert("publicAlert");
}
};
}(jQuery));
$(function() {
testModule.publicMethod();
});
But I don't really like this because the public methods can be overwritten. 但我真的不喜欢这个,因为公共方法可以被覆盖。 someone could go
testModule.publicAlert = function(){EVIL CODE OF DOOM;};
有人可以去
testModule.publicAlert = function(){EVIL CODE OF DOOM;};
and your internal workings will happily execute it. 并且您的内部工作将愉快地执行它。
I understand this is a little bit different from the module pattern, but I think it still offers the same benefits of encapsulation. 我知道这与模块模式略有不同,但我认为它仍然提供了封装相同的好处。 Public methods are declared as:
公共方法声明为:
this.methodName = function(){...}
the variable $this is assigned to the current instance (this), so you can use it from within private (or public) methods to access any methods or attributes on that instance. 变量$ this被分配给当前实例(this),因此您可以在私有(或公共)方法中使用它来访问该实例上的任何方法或属性。
Fork: 叉子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.