I am using the following namespacing pattern:
var MyNamespace = new function () {
var foo = function () {
bar();
};
var bar = function () {
alert("bar");
};
this.init = function () {
foo();
};
};
$(document).ready(function() {
MyNamespace.init();
});
JSLint complains that bar is used before it’s defined. However foo is not called until after bar has been declared. The code works fine with all the browsers I have tried: http://jsfiddle.net/jDKvz/
The pattern is per How do I declare a namespace in JavaScript?, second answer.
Do I need to fix something here, or should I just ignore JSLint?
I suspect that’s because of hoisting, variables and function declarations are hoisted onto top by the interpreter, it is likely that this is how it sees it:
Now inside the
foo = function () {bar();};thebar()isn’t yet parsed, it is just a variablebarnot a function to be called at that point.Having said that, if your code works fine, you can go with it, turning
strict modeon is also helpful.