JSLint抱怨重新定义undefined [英] JSLint complains about redefining undefined
问题描述
undefined
在技术上可以重新定义,因此它不是保留字。因此,我通常在匿名函数中编写代码,强制 undefined
成为未定义的变量,如下所示:
undefined
is technically able to be redefined, so it is not a reserved word. As a result, I usually write code in an anonymous function that forces undefined
to be an undefined variable, as so:
(function (undefined) {
"use strict";
var o = {
test: "testvalue"
};
if (o.test === undefined) {
// Do stuff here
} else {
// Do other stuff there
}
}());
然而, JSLint 提到以下错误:
Problem at line 1 character 15: Expected an identifier and instead saw 'undefined' (a reserved word).
为什么JSLint抱怨 undefined
是一个保留字,当代码可以任意重新定义变量?我知道你可以使用 typeof x ===undefined
;我只是想知道为什么这个方法不起作用。
Why does JSLint complain about undefined
being an reserved word, when code can arbitrarily redefine the variable? I know that you can use typeof x === "undefined"
; I just wanted to see why this method wouldn't work.
推荐答案
你的方法 工作。仅仅因为JSLint不喜欢它并不会使它成为一个主要的罪。
Your method does work. Just because JSLint doesn't like it doesn't make it a cardinal sin.
尝试 JSHint (为了更加理智)。
Try JSHint instead (for more sanity).
这篇关于JSLint抱怨重新定义undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!