Fix for JSLint “Unexpected 'this'.” error?

2019-07-20 03:47发布

问题:

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.

and

Unexpected 'this'.

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();

回答1:

The 2 things I can see which might cause this are:

  1. You have unnecessary {}s around your public methods. That is probably causing the expected statement error.
  2. 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); };


回答2:

{

    this.WritePV = function (name, value) { AccessPV(name, value); }

    this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); }
}

^^^

That's causing the error. Remove those braces and it fixes itself. It also wants you to use semicolons.

    this.WritePV = function (name, value) { AccessPV(name, value); };

    this.ReadPV = function (name, wValue) { return AccessPV(name, null, wValue); };