简体   繁体   English

JSLint不喜欢var

[英]JSLint doesn't like var

I was probing my code for errors with JSLint and so far I managed to fix all bugs except the problem when my global application variable is undefined. 我正在用JSLint来检查代码中的错误,到目前为止,我设法解决了所有错误,但未定义全局应用程序变量的问题除外。

I was using this code before JSlint which worked properly: 我在JSlint之前使用了此代码,它可以正常工作:

var APP = APP || (function {
    return {
        init: function () {

        }
    };
}(window.document));

then I would call 那我会打电话

APP.init();

to initialize. 初始化。 But JSlint doesn't like global variables so I changed my code to this: 但是JSlint不喜欢全局变量,因此我将代码更改为:

(function (global){
    var APP = {
        init: function () {

        }
    };
    return APP;
}(window.document));

This code passes the JSLint test with no problem, but then when I call APP.init(); 这段代码毫无问题地通过了JSLint测试,但是当我调用APP.init()时; it says that APP is used before it was defined, which is true. 它说APP是在定义之前使用的,这是事实。 What would you recommend so that I don't use global variables but still create my application object, while my code passes JSlint test? 您会如何建议我在代码通过JSlint测试的同时不使用全局变量,而是仍然创建应用程序对象?

    (function (global){
    var APP = {
        init: function () {

        }
    };
    return APP;
}(window.document));

the above creates a scope/closure, so that only code inside your anonymous function can access the APP variable. 上面创建了一个作用域/闭包,因此只有匿名函数内部的代码才能访问APP变量。 JSHint has settings so you can set acceptable globals (such as jquery etc), I'd add APP to that and if the rest pass you know you only have that one variable/module as global. JSHint有设置,因此您可以设置可接受的全局变量(例如jquery等),我将在其中添加APP ,如果其余的传递您知道只有一个变量/模块为全局变量。 If you are using any 3rd party libs chances are you already have several global vars anyway 如果您正在使用任何第三方库,那么您是否已经有几个全局变量

First code 第一个代码

In your original code, the actual error you would have gotten was 在您的原始代码中,您实际遇到的错误是

 #1 'APP' used out of scope.
    var APP = APP || (function() { // Line 1, Pos 11

It is not because JSLint hates global variables, but because it doesn't like the variable being assigned to it is defined in the same var statement. 这不是因为JSLint讨厌全局变量,而是因为它不喜欢在同一var语句中定义分配给它的变量。

Changing your code to use a different variable name would fix this problem 更改代码以使用其他变量名将解决此问题

var app = APP || (function() {...}(..));

but then, one would expect that APP is already defined in the current scope. 但随后,人们会期望在当前范围内已经定义了APP Otherwise you will get 否则你会得到

 #1 'APP' was used before it was defined.

Second code 第二码

It doesn't work because, APP is visible only inside the function object you created. 它不起作用,因为APP仅在您创建的函数对象内部可见。 It is not visible anywhere outside it. 它在外面的任何地方都不可见。 Since you want to define APP in the global scope, you just have to attach it to the window object, like this 由于要在全局范围内定义APP ,因此只需将其附加到window对象,就像这样

(function(global) {
    global.APP = {
        init: function() {

        }
    };
}(window));

Then 然后

APP.init()

will work fine. 会很好的工作。

Instead of going through all these, you can straight away define APP like this 无需经历所有这些,您可以像这样直接定义APP

window.APP = {
    init: function () {
        "use strict";
        ...
    }
};

Before you give up on JSLint , know that it doesn't mind globals at all; 在您放弃JSLint之前,请知道它根本不介意全局变量。 it just wants you to declare them with a special format so that others reading your code know exactly what's going on. 它只是想让您以特殊格式声明它们,以便其他阅读您的代码的人确切知道发生了什么。

The only "trick" here is if there's a chance your global APP isn't initialized yet and you really need to check for its "truthiness", as you do with APP = APP || (function... 这里唯一的“窍门”是,如果您的全局APP有可能尚未初始化,那么您真的需要检查其“真实性”,就像对APP = APP || (function... APP = APP || (function... . Then you do need to use the window (or whatever your global object is -- it's different if you use Node, for example) prefix trick. APP = APP || (function...然后,您确实需要使用window (或任何全局对象,例如,如果使用Node,则有所不同))前缀技巧。

Here's how: 这是如何做:

/*jslint white:true, sloppy:true, browser:true */
/*global APP */

window.APP = window.APP || (function () {
    return {
        init: function () {
            window.alert('JSLint doesn\'t like empty blocks');
        }
    };
}(window.document));


APP.init();

And JSLint is happy! JSLint很高兴!

I'm not against JSHint in theory, but that same flexibility that allows you to set all sorts of extra settings also tends to erode conformity, which is largely what I find the best benefit of a code linter to be. 从理论上讲,我并不反对JSHint,但是允许您设置各种额外设置的灵活性同样会削弱一致性,这在很大程度上是我发现代码棉签最大的好处。 Give both a good, solid run, and see which you like best! 进行良好,稳定的跑步,然后看看您最喜欢哪一个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM