Why are these methods generally available?
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'
a source to share
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.
a source to share
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.
a source to share