我刚刚开始使用 JSLint 来确保我创建的 JavaScript 代码至少符合一些标准,但我收到了一条令人困惑的消息:
JSLint: Unexpected 'that'.
该代码是实现进度条的解决方案的一部分,其中一部分是处理计时器和回调的对象,如下所示(这是从较大文件的开头摘录的内容,我可以添加整个文件如果需要):
var ProgressHandler = function () {
"use strict";
// Build a new object
var that = {};
// Add basic properties
that.taskid = 0;
that.timerid = 0; // Timer ID used to push refreshes
that.progressUrl = ""; // URL to invoke to read progress
that.interval = 500; // The interval for progress refresh
that.taskProgressCallback = null; // The user-defined callback that refreshes the UI
that.taskCompletedCallback = null; // The user-defined callback that finalizes the call
// Set progress url
that.setProgressUrl = function (url) {
that.progressUrl = url;
return this;
}
// Set frequency of refresh
that.setInterval = function (interval) {
that.interval = interval;
return this;
};
该消息出现在以 that.setInterval
开头的行上。 I 还有进一步的用途,但 JSLint 还表示它会在此时停止处理。我已尝试搜索此消息,但它并未在此处或 jslinterrors.com 上专门列出。
为什么会出现这种情况?我可以采取什么措施来解决它?或者应该忽略它?
最佳答案
问题似乎是在 that.setProgressUrl
的定义之后没有 ;
。更改为:
// Set progress url
that.setProgressUrl = function (url) {
that.progressUrl = url;
return this;
};
解决了报告的问题。然后,您会遇到一个问题,因为文件末尾缺少右大括号和分号,不确定这是否只是复制和粘贴问题。完整的脚本应如下所示:
var ProgressHandler = function () {
"use strict";
// Build a new object
var that = {};
// Add basic properties
that.taskid = 0;
that.timerid = 0; // Timer ID used to push refreshes
that.progressUrl = ""; // URL to invoke to read progress
that.interval = 500; // The interval for progress refresh
that.taskProgressCallback = null; // The user-defined callback that refreshes the UI
that.taskCompletedCallback = null; // The user-defined callback that finalizes the call
// Set progress url
that.setProgressUrl = function (url) {
that.progressUrl = url;
return this;
};
// Set frequency of refresh
that.setInterval = function (interval) {
that.interval = interval;
return this;
};
};
关于javascript - JSLint 意外 'that' 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27378276/