Does jshint understand Angular?

后端 未结 4 1412
甜味超标
甜味超标 2020-12-15 04:54

jshint is throwing an error when defining an angular module (or directive, or factory) as recommended by the Angular style guides (by John Papa or Todd Motto). For example,

4条回答
  •  爱一瞬间的悲伤
    2020-12-15 05:25

    Your code should work, but jshint is going to try and get you to code in a very strict manner. At the very least it's a "good practice" to have your functions defined before you use them. As I mentioned in the comment above, I think older javascript engines execute from top to bottom (can't remember for sure though and can't test) - so if you're going for wide-as-possible support you will want to listen to jshint.

    Something worth noting here is that if you use the var keyword to define your function, you will get an error - best explained by example:

    This works (http://jsfiddle.net/ryanwheale/kr8L825p/)

    (function() {
        try {
            foo();
        } catch(ex) {
            alert("ahhhhh, what's going on?!?!?\n\n" + ex.message);
        }
    
        function foo() {
            alert("I was hoisted to the top of this scope before execution :)");
        }
    })();
    

    ... but this doesn't (http://jsfiddle.net/ryanwheale/kr8L825p/4/)

    (function() {
        try {
            foo();
        } catch(ex) {
            alert("ahhhhh, what's going on?!?!?\n\n" + ex.message);
        }
    
        var foo = function() {
            alert("I was hoisted to the top of this scope before execution :)");
        }
    })();
    

提交回复
热议问题