为什么JSLint返回一个Unexpected 'String'消息

Why does JSLint return an Unexpected 'String' message?

本文关键字:Unexpected 消息 String 一个 返回 JSLint 为什么      更新时间:2023-09-26

我想在Javascript中添加一些额外的功能,并使用以下Javascript代码:

String.prototype.left = function (n) {
    "use strict";
    return this.substring(0, n);
}
String.prototype.right = function (n) {
    "use strict";
    return this.substring(this.length - n); 
}

然而,当我在JSLint上运行这段代码时,我得到了以下错误:我不太确定这个错误意味着什么,我不知道我是否应该改变我的JSLint设置来"避免"这个错误,或者我是否应该改变我的代码。

使用分号,我没有得到任何错误/警告:

String.prototype.left = function (n) {
    "use strict";
    return this.substring(0, n);
};
String.prototype.right = function (n) {
    "use strict";
    return this.substring(this.length - n); // <-- SPACE HERE
};

添加分号后,它警告我在上面添加注释的地方有一个意外的空格字符。因此,删除空格(和我的注释),它应该可以在那里验证。