I am using the following namespace template:
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 it is barused before it is defined. However, fooit is not called until it is declared bar. The code works fine with all the browsers I tried: http://jsfiddle.net/jDKvz/
bar
foo
Sample for How to declare a namespace in JavaScript? second answer.
Do I need to fix something, or just ignore JSLint?
, - , , , :
var MyNamespace = new function () { var foo; var bar; foo = function () { bar(); }; bar = function () { alert("bar"); }; this.init = function () { foo(); }; };
foo = function () {bar();}; bar() , bar, , .
foo = function () {bar();};
bar()
, , , , strict mode.
strict mode
: JS , .
JSLint , , . , , . JSLint.
:
var MyNamespace = new function () { var bar = function () { alert("bar"); }; var foo = function () { bar(); }; this.init = function () { foo(); }; };
bar - , , , . , jslint .
function bar() { ... }
Also, new Functionnot required, functionenough.
new Function
function
Edit:
It seems that in this case, to get rid of new, use the automatic call function (function(){}())or object literal{}
new
(function(){}())
{}