简体   繁体   English

在使用javascript模块模式时,如何从私有方法中调用公共方法?

[英]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: 叉子:

http://jsfiddle.net/FNjJq/ http://jsfiddle.net/FNjJq/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript模块模式中的私有函数中调用公共函数 - How do I call a public function from within a private function in the JavaScript Module Pattern 如何使用揭示模块模式将一个 JS 模块的公共方法调用到另一个模块中? - How to call a public method of one JS module into another, using the Revealing Module Pattern? Javascript在同一个对象中从私有方法调用公共方法 - Javascript calling public method from private one within same object JavaScript,在不使用eval的情况下将私有函数作为公共方法内的字符串调用(Revealing pattern) - JavaScript, call private function as a string inside public method without using eval (Revealing pattern) 如何从私有访问模块上的公共方法? - How can I access the public methods on a module from a private? 具有私有/公共方法的Javascript模块模式 - Javascript Module pattern with private/public methods 具有私有/公共方法的高效Javascript模块模式 - Efficient Javascript Module pattern with private/public methods 如何从JavaScript模块中的私有和公共函数访问公共财产? - How to access public property from private and public functions in a JavaScript module? 如何使用javascript中同一对象内的公共方法访问私有变量? - How to access private variable using a public method within same object in javascript? 使用John Resig的“简单JavaScript继承”,如何在一个方法中调用一个超级方法以及额外的代码? - Using John Resig's “simple JavaScript inheritance” how can I call a super method PLUS extra code from within a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM