문제

(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