修复了JSLint“意外的'this'.".错误? [英] Fix for JSLint "Unexpected 'this'." error?
本文介绍了修复了JSLint“意外的'this'.".错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取以下代码,使其成为 jslint兼容,但是由于以下两个错误而卡住了: /p>
I am trying to get the following code to become jslint-compliant, but am stuck on the following two errors:
希望看到一条语句,而看到一个块.
Expected to see a statement and instead saw a block.
和
意外的"this".
Unexpected 'this'.
我应该对我的代码进行哪些更改以使JSLint满意?
What changes should I make to my code to make JSLint happy?
var pvAccess = {};
pvAccess.Func = function () {
"use strict";
function AccessPV(name, rValue, wValue) {
var url = '/goform/ReadWrite',
data = 'redirect=/response.asp&variable=' + escape(name),
xmlHttp = null,
wValue = null;
if (rValue !== null && rValue !== "") {
data += '&value=' + escape(rValue);
data += "&write=1";
} else {
data += '&value=none';
data += "&read = 1";
}
try {
// Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
// MS Internet Explorer (ab v6)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
try {
// MS Internet Explorer (ab v5)
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e3) {
xmlHttp = null;
}
}
}
if (xmlHttp) {
xmlHttp.open('POST', url, 1);
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4) {
if (wValue !== null) {
wValue[3] = xmlHttp.responseText;
wValue[3] = wValue[3].replace("<!-- B&R ASP Webserver -->", "");
// value attribute of node
wValue.value = wValue[3];
return wValue;
}
}
};
xmlHttp.send(data);
}
}
// public
{
this.WritePV = function (name, value) { AccessPV(name, value); }
this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); }
}
}
pvAccess = new pvAccess.Func();
推荐答案
我看到的可能导致此问题的两件事是:
The 2 things I can see which might cause this are:
- 您的公共方法周围没有多余的
{}
.这可能导致预期的语句错误. - 您的公共方法不仅是函数,而且是赋值语句,因此您应在函数定义之后以分号结束.没有这个,lint会将其读取为1行代码(
this.WritePV = function { ... }this.ReadPV
),因此是意外的".
- You have unnecessary
{}
s around your public methods. That is probably causing the expected statement error. - Your public methods are assignment statements not just functions, so you should end the line with a semicolon, after the function definition. Without this, lint is reading it as 1 line of code (
this.WritePV = function { ... }this.ReadPV
), hence the "unexpected this".
因此,您需要更改公共方法,使其看起来像这样:
So you need to change your public methods to look like this:
// public
this.WritePV = function (name, value) { AccessPV(name, value); };
this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); };
这篇关于修复了JSLint“意外的'this'.".错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文