0
// first.js
$(document).ready(function() {
   var MyNamespace = {};
});

// second.js
$(document).ready(function() {
   console.log(MyNamespace);
});

Running this script I'm getting error Uncaught ReferenceError: MyNamespace is not defined. I suppose, I'm getting this error because definition of MyNamespace and MyNamespace calling are in different scopes. How do I solve this problem?

I need to create namespace inside $(document).ready() wrapper because functions in this namespace will use jQuery methods etc.

What is the best practice?

Thank you!

6
  • Why do you use two calls to ready()? Commented Nov 22, 2011 at 9:30
  • you need to define the variable outside the document ready function, then assign a value within the function. Or, take out the var Commented Nov 22, 2011 at 9:30
  • @Tomalak, one $(document).ready() I'm using in first.js and second in second.js Commented Nov 22, 2011 at 9:31
  • @Abe Petrillo, but I'm going to use $ inside my namespace methods. So I have to be sure that jQuery is loaded and DOM is ready. It is the main reason why I'm asking this question... Commented Nov 22, 2011 at 9:32
  • @Kirzilla - You can still declare the namespace var outside of the ready event handlers. As long as code that requires the DOM to be ready stays inside, it won't make any difference. Commented Nov 22, 2011 at 9:37

6 Answers 6

2

There are two things you need to change:

  1. Put the MyNamespace in a global scope;
  2. Avoid redefining MyNamespace in each file;

You put var MyNamespace = MyNamespace || {}; in front of both your js files. It decalres MyNamespace as an object if it isn't defined before.

// first.js
var MyNamespace = MyNamespace || {};
$(document).ready(function() {
   console.log(MyNamespace);
});

// second.js
var MyNamespace = MyNamespace || {};
$(document).ready(function() {
   console.log(MyNamespace);
});
Sign up to request clarification or add additional context in comments.

Comments

2

It should work if you define your variable outside of the document ready handler -

var MyNamespace = {};
$(document).ready(function() {
    console.log(MyNamespace);
});

Comments

0

Create the namespace outside the scope, then add methods to it within the scope:

var MyNamespace = {};

$(document).ready(function() {
   MyNamespace.MyFunction = function () { ... };
});
$(document).ready(function() {
   console.log(MyNamespace);
});

Comments

0

Create it outside and update it inside:

var MyNamespace = {};

$(document).ready(function() {
   MyNamespace.Example = new function() { // do something };
});

$(document).ready(function() {
   console.log(MyNamespace);
});

Comments

0

Using var defines a variable local to the function, which is not visible outside of this scope. Leaving out var would define a global variable, accessible in the other function as well. In general you should rather avoid using global variables, but if that's what you need, simply say MyNamespace = {};

Comments

-1

Use the 'window' object.

$(document).ready(function () {
    window['x'] = 'test';
});
$(document).ready(function () {
    alert(x);
});

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.