Access my public methods from my namespace

I am creating my own namespace in JavaScript ...

(function(window){
    (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }

        return (window.myNamespace = window.my = myNamespace)
    }());
})(window);

I am new to such advanced JavaScript methods, and I am trying to find a better way to call public methods from my namespace. My public methods seem to thisset a value myNamespace.

Should I call public methods like ...

AnotherPublicMethod: function(){
   this.somePublicMethod()
}

or...

AnotherPublicMethod: function(){
   my.somePublicMethod();
}

is there any difference

+3
source share
1 answer

, this, , my, , my window.

.

EDIT:

, this , , Activation.

, :

my.anotherPublicMethod();

:

var test = my.anotherPublicMethod;
test();

, my . , . myNamespace .


, , , .

:

return (window.myNamespace = window.my = myNamespace)

... myNamespace.

, ?

(function(window){
    window.myNamespace = window.my = (function(){
        var myNamespace = {
            somePublicMethod: function(){
            },
            anotherPublicMethod: function(){
            }
        }
        return myNamespace;
    }());
})(window);
+3

Source: https://habr.com/ru/post/1783707/


All Articles