“[变量]在定义之前使用”错误 [英] "[Variable] was used before it was defined" error

查看:125
本文介绍了“[变量]在定义之前使用”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个这样的错误,我不知道如何正确解决它,问题是我有很多javascript文件(分离以便于维护)和我包含插件等等。

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.

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

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

这只是定义快捷方式

shortcut = {.......

然后当我在我的代码中使用它时

then when I in my code use it like

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

jslint会抱怨


JS Lint:'快捷'在定义之前使用过。

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

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

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

推荐答案

如果变量是由另一个文件定义的,你可以告诉它JSLint通过以下格式提供评论:

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

/*global shortcut*/

您可以通过逗号分隔它们来为多个变量执行此操作。附加 true false (默认为 false )将指定当前文件是否可以重新分配变量:

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:

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






您缺少 var 关键字,该关键字用于定义全局和函数范围的变量。


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

var shortcut = { }

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

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

可以通过省略 var 关键字来创建隐式全局变量,但它是高度皱眉,根本不推荐。如果需要从内部作用域创建全局变量,可以将对象添加到窗口,或者根据上下文, this

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:

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

defineShortcut();

这篇关于“[变量]在定义之前使用”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆