最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSLint complains about redefining undefined - Stack Overflow

programmeradmin0浏览0评论

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

However, JSLint mentions the following error:

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? I know that you can use typeof x === "undefined"; I just wanted to see why this method wouldn't work.

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

However, JSLint mentions the following error:

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? I know that you can use typeof x === "undefined"; I just wanted to see why this method wouldn't work.

Share Improve this question asked Nov 17, 2011 at 23:07 Kevin JiKevin Ji 10.5k4 gold badges43 silver badges65 bronze badges 4
  • "Why does JSLint complain about undefined being an reserved word" Because it is a reserved word! – epascarello Commented Nov 18, 2011 at 0:00
  • 4 @epascarello - can you please give a link to a JavaScript/ECMAScript reference that lists reserved words? I don't think undefined is on the list, and certainly the code above works. You can also just say undefined = "not undefined any more" and it will work. – nnnnnn Commented Nov 18, 2011 at 1:13
  • 2 @nnnnnn @epascarello According to Mozilla docs, undefined is not a reserved word. – Kevin Ji Commented Nov 18, 2011 at 4:48
  • This code is not actually redefining undefined at all, it is only passing undefined as a parameter to a closure. You probably want var undefined='something else'; instead. – Talvi Watia Commented Jan 19, 2013 at 0:15
Add a comment  | 

4 Answers 4

Reset to default 17

'undefined' was declared an immutable property of the global object in ECMA-262 Edition 5 Section 15.1.1.3, published in December 2009.

By using 'undefined' as a parameter name in a function, you are attempting to mutate it with whatever is passed to the function. So technically, the error lies with browsers being slow to adopt the standard, and JSLint is correct.

Your method does work. Just because JSLint doesn't like it doesn't make it a cardinal sin.

Try JSHint instead (for more sanity).

(function() { var undefined = 'foo'; console.log(undefined, typeof undefined); })();

According to MDN, it's actually not a reserved word in JavaScript and could be redefined.

undefined is a reserved word. It's like trying to name a variable false or true. Your value you are passing into your function:

function(undefined) {...}

needs to be named something else, like

function(myVariable) {...}

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...

发布评论

评论列表(0)

  1. 暂无评论