Pergunta

(function( Pbr) {

    Pbr.ShowHomePage = function() {
        console.log("ShowHomePage called")
    }

    function privateFunc() {
        console.log("Showtitle");
    }

    return {
        ShowTitle : privateFunc
    }

}(Pbr = Pbr || {}));


Pbr.ShowHomePage()
Pbr.ShowTitle()

I am trying implement Revealing pattern. But it fails to work.
ShowHomePage is working fine, but ShowTitle is not working

Foi útil?

Solução

Looks like you want to reveal your methods by attaching them to the argument, not by returning them. Use

(function(Pbr) {

    Pbr.ShowHomePage = function() {
        console.log("ShowHomePage called")
    }

    function privateFunc() {
        console.log("Showtitle");
    }
    Pbr.ShowTitle = privateFunc; // not very private, btw

}(Pbr = Pbr || {}));

If you want to return an object literal, you will need to assign the result of the IEFE, and overwrite existing Pbr values. It would look like

var Pbr = (function() {

    function privateFunc() {
        console.log("Showtitle");
    }

    return {
        ShowTitle: privateFunc
        ShowHomePage: function() {
            console.log("ShowHomePage called")
        }
    }
}());

Outras dicas

As Felix said in comments, you need to use the return value.

Either assign the full module to Pbr and reveal ShowHomePage as well (as you have done for privatefunc).

Or, add Pbr.ShowTitle = privatefunc; in your module and remove the return statement.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top