[英]JSLint complains about redefining undefined
undefined
is technically able to be redefined, so it is not a reserved word. undefined
在技术上可以重新定义,因此它不是保留字。 As a result, I usually write code in an anonymous function that forces undefined
to be an undefined variable, as so: 因此,我通常在匿名函数中编写代码,强制
undefined
为未定义的变量,如下所示:
(function (undefined) {
"use strict";
var o = {
test: "testvalue"
};
if (o.test === undefined) {
// Do stuff here
} else {
// Do other stuff there
}
}());
However, JSLint mentions the following error: 但是, JSLint提到了以下错误:
Problem at line 1 character 15: Expected an identifier and instead saw 'undefined' (a reserved word).
Why does JSLint complain about undefined
being an reserved word, when code can arbitrarily redefine the variable? 当代码可以随意重新定义变量时,为什么JSLint会抱怨
undefined
为保留字? I know that you can use typeof x === "undefined"
; 我知道你可以使用
typeof x === "undefined"
; I just wanted to see why this method wouldn't work. 我只是想知道为什么这种方法不起作用。
'undefined' was declared an immutable property of the global object in ECMA-262 Edition 5 Section 15.1.1.3 , published in December 2009. 'undefined'在2009年12月发布的ECMA-262 Edition 5 Section 15.1.1.3中被声明为全局对象的不可变属性。
By using 'undefined' as a parameter name in a function, you are attempting to mutate it with whatever is passed to the function. 通过在函数中使用'undefined'作为参数名称,您试图使用传递给函数的任何内容来改变它。 So technically, the error lies with browsers being slow to adopt the standard, and JSLint is correct.
从技术上讲,错误在于浏览器采用标准的速度很慢,而且JSLint是正确的。
(function() { var undefined = 'foo'; console.log(undefined, typeof undefined); })();
根据MDN,它实际上不是JavaScript中的保留字,可以重新定义。
undefined
is a reserved word. undefined
是一个保留字。 It's like trying to name a variable false
or true
. 这就像尝试将变量命名为
false
或true
。 Your value you are passing into your function: 您传递给函数的价值:
function(undefined) {...}
needs to be named something else, like 需要被命名为其他东西,比如
function(myVariable) {...}
UPDATE UPDATE
Looks like it might not be actually reserved as I originally read but perhaps it's just a term that JSLint thinks should be reserved... 看起来它可能实际上并没有像我最初阅读的那样保留,但也许它只是JSLint认为应该保留的术语......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.