在定义“错误”之前使用“[变量]”

时间:2022-11-05 17:09:50

I have several of these errors and I am not sure how to "properly" solve it, the thing is that I have many javascript files (Seperated for easier maintainability) and I include plugins et cetera.

我有几个这样的错误,我不知道如何“正确”地解决它,问题是我有许多javascript文件(为了更容易维护而分离),我包括插件等等。

So in this example I use shortcut which is from http://www.openjs.com/scripts/events/keyboard_shortcuts/

所以在这个例子中,我使用的快捷方式是http://www.openjs.com/scripts/events/keyboard_shortcuts/

This just defines shortcut to

这就定义了快捷方式

shortcut = {.......

then when I in my code use it like

当我在代码中使用它时

 shortcut.add("F1", function () { showDialog(); }, { 'type': 'keydown', 'propagate': false, 'target': editor_document });

jslint will complain that

jslint会抱怨

JS Lint: 'shortcut' was used before it was defined.

JS Lint:在定义之前使用了“快捷方式”。

I also have my own code where I use functions declared in other files, so what is the "right" way to solve this

我也有自己的代码,使用在其他文件中声明的函数,那么解决这个问题的“正确”方法是什么呢

2 个解决方案

#1


7  

If the variable is defined by another file, you can tell JSLint by providing a comment in the following format:

如果变量是由另一个文件定义的,您可以通过提供以下格式的注释来告诉JSLint:

/*global shortcut*/

You can do this for a number of variables by comma separating them. Appending : and true or false (defaults to false) will specify whether the variable can be reassigned by the current file:

你可以用逗号分隔一些变量。追加:true或false(默认值为false)将指定变量是否可以由当前文件重新分配:

/*global shortcut:false, otherVar:true*/

You're missing the var keyword, which is used to define a variable for the global and function scopes.

您丢失了var关键字,它用于定义全局和函数作用域的变量。

var shortcut = { }

You need to use var for every variable defined, else you'll run into a mass of problems.

对于定义的每个变量都需要使用var,否则会遇到大量的问题。

It is possible to create implicit globals by omitting the var keyword, but it's highly frowned upon and not at all recommended. If you need to create a global variable from an inner scope, you can add the object to window or, depending on the context, this:

可以通过省略var关键字来创建隐式全局变量,但这是非常不允许的,完全不推荐。如果需要从内部范围创建全局变量,可以将对象添加到窗口中,或者根据上下文的不同,添加以下内容:

function defineShortcut() {
    window.shortcut = {};
    /* or this.shortcut = {}; */
}

defineShortcut();

#2


0  

you must declare a variable shortcut with var keyword before using it, or else it will be a global variable which are considered (and are in fact) evil.

在使用var关键字之前,必须声明一个带有var的变量快捷方式,否则它将是一个全局变量,被认为(实际上是)有害的。

var shortcut;

shortcut = { ...

#1


7  

If the variable is defined by another file, you can tell JSLint by providing a comment in the following format:

如果变量是由另一个文件定义的,您可以通过提供以下格式的注释来告诉JSLint:

/*global shortcut*/

You can do this for a number of variables by comma separating them. Appending : and true or false (defaults to false) will specify whether the variable can be reassigned by the current file:

你可以用逗号分隔一些变量。追加:true或false(默认值为false)将指定变量是否可以由当前文件重新分配:

/*global shortcut:false, otherVar:true*/

You're missing the var keyword, which is used to define a variable for the global and function scopes.

您丢失了var关键字,它用于定义全局和函数作用域的变量。

var shortcut = { }

You need to use var for every variable defined, else you'll run into a mass of problems.

对于定义的每个变量都需要使用var,否则会遇到大量的问题。

It is possible to create implicit globals by omitting the var keyword, but it's highly frowned upon and not at all recommended. If you need to create a global variable from an inner scope, you can add the object to window or, depending on the context, this:

可以通过省略var关键字来创建隐式全局变量,但这是非常不允许的,完全不推荐。如果需要从内部范围创建全局变量,可以将对象添加到窗口中,或者根据上下文的不同,添加以下内容:

function defineShortcut() {
    window.shortcut = {};
    /* or this.shortcut = {}; */
}

defineShortcut();

#2


0  

you must declare a variable shortcut with var keyword before using it, or else it will be a global variable which are considered (and are in fact) evil.

在使用var关键字之前,必须声明一个带有var的变量快捷方式,否则它将是一个全局变量,被认为(实际上是)有害的。

var shortcut;

shortcut = { ...