在使用javascript模块模式时,如何从私有方法中调用公共方法? [英] How can i call a public method from within a private one when using the javascript Module Pattern?

查看:75
本文介绍了在使用javascript模块模式时,如何从私有方法中调用公共方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从私有方法调用公共方法,但属性this指的是窗口对象。

I would like to call a public method from a private one but the property "this" refers to the window object.

请注意我正在尝试应用模块模式。您可以在 jsfiddle.net 找到一个有效的代码示例

Please note i am trying to apply the module pattern. You can find a working code example at 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();
});


推荐答案

如果你想做到这一点,你需要像私有函数一样声明'public'函数,然后将其公开为public。像这样:

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. 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();
});


我也鼓励你养成点击在jsfiddle顶部的'jslint'按钮,你的代码缺少几个分号,你还重新声明了你的appendText函数中的'text'变量(它已经被传入)

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)

此外,您使用模块模式的方式与我学习模式的方式略有不同。你有参考资料的链接吗?

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?

这就是我所知道的模块模式: http://jsfiddle.net/sVxvz/
[/ Edit]

This is how I would have done the module pattern as I know it: 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();
});

但我真的不喜欢这个,因为公共方法可以被覆盖。
有人可以去 testModule.publicAlert = function(){EVIL CODE OF DOOM;}; ,你的内部工作将很乐意执行它。

But I don't really like this because the public methods can be overwritten. someone could go testModule.publicAlert = function(){EVIL CODE OF DOOM;}; and your internal workings will happily execute it.

这篇关于在使用javascript模块模式时,如何从私有方法中调用公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆