简体   繁体   English

如何在JavaScript中定义未定义?

[英]How do I defined undefined in JavaScript?

There's this IIFE trick to get around people that have defined undefined to something. 有一个IIFE技巧可以绕开那些将undefined为某种东西的人。 It goes something like this: 它是这样的:

(function (undefined) {
    // ...
})();

How do I become this person? 我如何成为这个人? Is it even possible to define undefined anymore? 甚至可以定义undefined了吗?

If I open my console (FF or Chrome), it will look something like this: 如果打开控制台(FF或Chrome),它将看起来像这样:

undefined = 5  // => 5
undefined      // => undefined

在现代浏览器中,您不必为未定义的值赋值。

Well I agree - there is no way to define it. 好吧,我同意-无法定义它。 And I will try to explain why: 我将尝试解释原因:

console.log(Object.getOwnPropertyDescriptor(window,"undefined"))

which, of course, prints the following: 当然,它会打印以下内容:

Object {value: undefined, writable: false, enumerable: false, configurable: false}

According to MDN if the property writable is false then you cannot change it. 根据MDN,如果writable属性为false则无法更改它。 Quite obvious, isn't it? 很明显,不是吗?

If you try to change your scope (do it inside of the function), you will be the owner of the scope and perhaps you can define the name as you like. 如果您尝试更改范围(在函数内部进行操作),则您将是范围的所有者,也许您可​​以根据需要定义名称。 But as far as you are on window scope - no way. 但是就您在window范围而言-没办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM