(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

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top