Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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();
});
like image 666
Ota Avatar asked Dec 21 '10 23:12

Ota


People also ask

Can we call public method from private method?

If a private method must call a public method, then the content of the public method should be taken and placed in a private method, which both methods can then call.

How do you make a public function in JavaScript?

publicMethod = function () {...}; in a constructor function creates a public own method to every instance created by using that constructor.

How do you make a method private in JavaScript?

To make a public method private, you prefix its name with a hash # . JavaScript allows you to define private methods for instance methods, static methods, and getter/setters. The following shows the syntax of defining a private instance method: class MyClass { #privateMethod() { //... } }

Are there private functions in JavaScript?

Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself.


1 Answers

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

[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)

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]

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;}; and your internal workings will happily execute it.

like image 50
david Avatar answered Oct 05 '22 23:10

david