var utils = function() {
function getMyPrivateName() {
return "Caoimhin";
}
return {
messages: {
getMyPublicName: function getMyPublicName() {
return "Kevin";
},
sayHello: function() {
document.writeln("hello " + getMyPublicName() + "<br/>");
document.writeln("hello " + getMyPrivateName() + "<br/>");
}
}
};
} ();
utils.messages.sayHello();
I am playing around with javascript namespaces and have encountered unexpected behaviour. I develop mostly in IE as that is the target browser for our intranet application.
In IE the above, when included on a blank page, outputs:
hello Kevin
hello Caoimhin
In FF the script encounters an error:
getMyPublicName is not defined
If I comment out the offending line:
//document.writeln("hello " + getMyPublicName() + "<br/>");
FF outputs:
hello Caoimhin
So I know it can access the private function...
Can anyone explain why this is happening? And what I need to do in order to have a cross browser solution similar to the above..
I know I could write something like:
document.writeln("hello " + utils.messages.getMyPublicName() + "<br/>");
but would prefer not to....
Thanks in advance, Kevin