I want to create a namespace to organise all of my JavaScript. I can't see a way, using the code sample below, to create a local function so that it can't be called except from within this Object.
window.JD = window.JD || {};
JD.Guid = {
newGuid : function() {
return JD.Guid.s4(); // Would like this to be this.s4();
},
s4 : function() {
return Math.floor((1 + Math.random()) * 0x10000);
}
}
I have tried using JD.Guid = function() {...} but that doesn't work either. The console tells me that the function is undefined.
I'd like to be able to call JD.Guid.newGuid() from anywhere within my site.
JDthen try callingwindow.JD.Guid.newGuid(), otherwise, I don't see anything wrong, you can actually callJD.Guid.newGuid()from anywhere in your site, (if no 'JD' is there in the current scope or parent hierarchy scope apart from the window's one)