The Namespacing parameter causes the JSLint function to be used before it detects an error.

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/

Sample for How to declare a namespace in JavaScript? second answer.

Do I need to fix something, or just ignore JSLint?

+3
source share
3 answers

, - , , , :

var MyNamespace = new function () {
    var foo;
    var bar;

    foo = function () {
        bar();
    };

    bar = function () {
        alert("bar");
    };

    this.init = function () {
        foo();
    };
};

foo = function () {bar();}; bar() , bar, , .

, , , , strict mode.

+1

: JS , .

JSLint , , . , , . JSLint.

:

var MyNamespace = new function () {
    var bar = function () {
        alert("bar");
    };

    var foo = function () {
        bar();
    };

    this.init = function () {
        foo();
    };
};
0

bar - , , , . , jslint .

function bar() {
    ...
}

Also, new Functionnot required, functionenough.

Edit:

It seems that in this case, to get rid of new, use the automatic call function (function(){}())or object literal{}

0
source

All Articles