开发者

Why are these methods public?

开发者 https://www.devze.com 2022-12-30 00:28 出处:网络
My javascript lo开发者_StackOverflowoks like the following.I don\'t understand why these methods are all public though?

My javascript lo开发者_StackOverflowoks like the following. I don't understand why these methods are all public though?

Something.RegisterNamespace("One.ABC");

    (function(ABC) {

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

      };

    })(One.ABC);

So now I can do:

One.ABC.SayHello();


The only effective way to have private methods is by using a closure.

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

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


You are adding the SayHello function into the object being passed in, which is One.ABC. What else are you expecting? 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're trying to accomplish...

EDIT:

Here's how I would rewrite your code to do what I think you want:

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'


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 (actually, an Object in the global namespace). The first property of One is ABC. ABC is an Object too and has one property, the public methodSayHello. If you wanted SayHello to be private, this could be a 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's 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 is an object too, but it is created using an anonymous constructor function instantiated on creation (a singleton pattern I think it's called). Within that constructor SayHello is now a private (not publicly accessible) function. You'll have to assign some public method to access SayHello (here: Two.ABC.Hi), otherwise it will be completely hidden. In this example, because SayHello is defined within the anonymous function scope, it is accessible for the methods that anonymous function returns, which in turn are accessible to the parent scopes (ABC and TWo). In other words, the function SayHello is enclosed by the methods the singleton returns.


I hope the function's name and the Name space's name are same, so those can be referred as public


RegisterNamespace is not standard JS. But that looks like it is creating an object "One" with a property of "ABC"

The anonymous function is binding its inner function to the object "One" via the property "ABC"

So you end up with:

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


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

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

here, you're defining the property SayHello to be a function that executes the alert statement when called.

edit: perhaps you're being thrown off by the structure of the code? this section

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

  };

})(One.ABC);

is equivalent to

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

addHello(One.ABC);

the only difference is that in your example, the function is being defined inline and then run immediately.

(...)(parameters);

defining it inline just make the function available for one-off use, where in my example you could use the same function to define a SayHello method for many objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号