3

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.

4
  • Try:console.log(JD.Guid.newGuid()); Commented Aug 29, 2017 at 15:21
  • How do you want to access your function? Commented Aug 29, 2017 at 15:21
  • Thanks @JohnnyAW. Question updated. Commented Aug 29, 2017 at 15:23
  • if your local scope (because you want to call from anywhere) have JD then try calling window.JD.Guid.newGuid(), otherwise, I don't see anything wrong, you can actually call JD.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) Commented Aug 29, 2017 at 15:26

3 Answers 3

1

I think you're asking about module design pattern and in this case your code must looks like:

window.JD = window.JD || {};
JD.Guid = (function () {
    // This function is private,
    // you can re-use it only from public accessible method.
    var privateMethod = function() {
        return 'private';
    };
    return {
        newGuid : function() {
            return JD.Guid.s4();
        },
        newGuidThroughtThis : function() {
            return this.s4();
        },
        privateMethod: function() {
            return privateMethod();
        },
        s4 : function() {
            return Math.floor((1 + Math.random()) * 0x10000);
        }
    }
})();

Now you can re-use your module and do something like:

console.log(JD.Guid.s4());
console.log(JD.Guid.newGuid());
console.log(JD.Guid.newGuidThroughtThis());

as output you'll get something like: 118723

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is exactly what I'm looking for and found this link which may help others in the future toddmotto.com/mastering-the-module-pattern
Cool! Yes, link is helpful.
0

I usually do something like this:

var JD = (function(GLOBAL_JD){
      if(GLOBAL_JD === undefined) {
        var jd = {
          GUID: {
            newGuid: newGuid          
          }
        };

        return jd;
      }

  function newGuid() {
    return s4(); // Would like this to be this.s4();
  }

  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000);
  }
})(JD);

console.log(JD.GUID.newGuid());

http://jsbin.com/jodifeb/edit?js,console,output

Comments

0

I'm not sure that I'm following. But you can create a local function which is not accessible in global namespace by creating a context wrapping your code in a function:

(function() {
  window.JD = window.JD || {};

  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000);
  }

  JD.Guid = {
    newGuid: function() {
      return s4();
    }
  }
})();

1 Comment

Yes it does although another answer talks about the module design pattern, which is exactly the sort of thing I'm after. Your answer does match the question though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.