Am I doing something wrong or is this just not possible:
(function(namespace,undefined)
{
//Private properties and methods
var foo="bar";
function test(){return foo;}
//Public properties and methods
namespace.foobar=foo+"123";
namespace.showFoo=function(){return test();};
})(window.namespace=window.namespace || {});
Then I try to “extend” the above namespace and add a new method:
(function(namespace,undefined)
{
//Public method
namespace.sayGoodbye=function()
{
alert(namespace.foo);
alert(namespace.bar);
alert(test());
}
})(window.namespace=window.namespace || {});
The alert shows undefined for the properties and throws an error for the test() method.
Thanks.
Why would you expect to have
fooandbaravailable ? Those identifiers are never assigned to yournamespaceobject anywhere.Any variable that is declared with
varis only available in the Function(-Context) of the current Activation/Variable Object. Same goes forfunction declarations, in your case,test(). Both these are only stored within the AO from the first anonymous function and are not stored within yournamespaceobject. You would have to explicitly assign the values