我刚刚开始使用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
开头的行上。我还有其他用途,但JSLint也表示此时它会停止处理。我已尝试搜索此邮件,但此处未在此处或jslinterrors.com上列出。
为什么会出现这种情况,我该怎么做才能解决这个问题?或者它应该被忽略?
答案 0 :(得分:3)
问题似乎是在;
的定义之后没有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;
};
};