Why are these methods generally available?

My javascript looks like this. I don't understand why these methods are public, though?

Something.RegisterNamespace("One.ABC");

    (function(ABC) {

      ABC.SayHello = function() {
             alert('hello');

      };

    })(One.ABC);

      

So now I can do:

One.ABC.SayHello();

      

+2


a source to share


6 answers


You add a function SayHello

to the passed object which is One.ABC

. What else do you expect? If you want a private function, define it inside your anonymous function ( var SayHello = function(){...}

) without adding it to the object. Not sure what you are trying to accomplish ...

EDIT



This is how I would rewrite your code to do what I think you need:

One = {};
One.ABC = {};

(function(ABC) {
    var PrivateHello = function(){
        alert('hello');
    };
    ABC.PublicHello = function() {
        PrivateHello();
    };
})(One.ABC);

One.ABC.PublicHello(); // alerts 'hello'
One.ABC.PrivateHello(); // error 'One.ABC.PrivateHello is not a function'

      

+1


a source


The only efficient way to have private methods is to use a closure.



function MyClass() {
    var privateMethod = function() {
        return 0;
    };

    this.publicMethod = function() {
        return privateMethod();
    };
}

      

+2


a source


Your code can also be written as:

var One = {
      ABC:{
       SayHello: function() {
         alert('hello');
       }
      }
};
One.ABC.SayHello(); //=> hello

      

This variable definition creates a (pseudo) namespace One

(in fact, an object in the global namespace). The first property One

is ABC

. ABC

is also an object and has one property, a public method SayHello

. If you want to SayHello

be private, this might be the way to do it:

var Two = {
    ABC: ( function(){
        // SayHello is a private function within this
        // anonymous function 
        // it can only be accessed by a public method 
        // you create (here it the method Hi)
        function SayHello() {
          alert('hello from Two.ABC');
        }
        return {
                SayHello: function(){alert('you are not allowed to say Hello!');},
                Hi: SayHello
               };
        } )()
    }
    Two.ABC.SayHello(); //=> you are not allowed to say Hello!
    Two.ABC.Hi(); //=> hello from Two.ABC

      

Now Two.ABC

also an object, but it is instantiated using the anonymous constructor function created on creation (the singleton template I think it was calling). Inside this constructor is SayHello

now a private (not public) function. You will have to assign some public method to access SayHello

(here :) Two.ABC.Hi

otherwise it will be completely hidden. In this example, because it SayHello

is defined within the scope of anonymous functions, it is available to methods returned by the anonymous function, which in turn are available to parent scopes (ABC and TWo). In other words, the function is SayHello

wrapped in the methods returned by the singleton.

+1


a source


I hope the function name and namespace name are the same, so they can be passed as public

0


a source


RegisterNamespace is not standard JS. But it looks like it creates object "One" with property "ABC"

Anonymous function binds its inner function to the "One" object through the "ABC" property

So you are done:

var One = {"ABC" : { "SayHello" : function(){alert('hello')}}}

      

0


a source


all properties / objects of an object are public. the sample code you posted is equivalent to

Something.RegisterNamespace("One.ABC");
One.ABC.SayHello = function() { alert('hello'); };

      

here you define a property SayHello

as a function that executes an alert statement when called.

edit: maybe you're getting discarded by the structure of the code? this section

(function(ABC) {
  ABC.SayHello = function() {
         alert('hello');

  };

})(One.ABC);

      

equivalent to

function addHello(pObject) {
    pObject.SayHello = function() {
        alert('hello');
    };
}

addHello(One.ABC);

      

The only difference is that in your example the function is defined inline and then executed right away.

(...)(parameters);

      

by defining it in line, it's just to make the function disposable, where in my example you can use the same function to define a method SayHello

for many objects.

0


a source







All Articles