質問

(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