0

I have one more question, if I use jquery.js in the head section then do I need to put document. ready as following or it will do without it.

var myModule = (function () {
  "use strict";

  var _privateProperty = "Hello World";
  var publicProperty = "I am a public property";

  function _privateMethod() {
    console.log(_privateProperty);
  }

  function publicMethod() {
    _privateMethod();
  }

  return {
    publicMethod: publicMethod,
    publicProperty: publicProperty,
  };
})();

$(document).ready(function () {
  myModule.publicMethod(); // outputs 'Hello World'
  console.log(myModule.publicProperty); // outputs 'I am a public property'
  console.log(myModule._privateProperty); // is undefined protected by the module closure
  myModule._privateMethod(); // is TypeError protected by the module closure
});
2
  • if your functions are jQuery, you need put them inside of .ready or .load fnc because you need jQuery initialized Commented Jul 21, 2020 at 11:55
  • Depends on what your module does. The module in your snippet does not interact with the DOM at all, so it doesn't need jQuery either. Commented Jul 21, 2020 at 12:24

1 Answer 1

1

Per jQuery docs:

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you.

This is only true for DOM operations. If you need to access the DOM, then yes, you need to wait for the event.

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

Comments

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.