2017-09-15 63 views
2

我对声明变量undefined和评估真值if-else表达式给出了一些想法。我发现在全球范围内这是不可能的。但在MDN中,我发现了关于undefined数据类型的一些有趣的见解。我直接引用MDN未定义为本地范围内的有效变量名称

undefined是全局对象的一个​​属性;即它是全球范围内的变量 。

它没有说任何关于本地范围的内容。这意味着我可以在本地范围内创建一个。所以,我继续将这种见解进行测试。我创建了一个对象并为其指定一个方法

var person = { 
    method : function(){ 

     var undefined = 2 

     if(undefined){ 
      console.log("undefined works as a variable") 
     } 

    } 
} 

person.method() 

然后猜猜看! if语句通过测试并将console.log()中的字符串打印在控制台上。这可能是一件危险的事情,当然也是一种不好的做法。有什么办法来阻止在JavaScript的本地范围中声明一个以undefined关键字命名的变量吗?谢谢!

+0

的[VAR未定义= TRUE;]可能的复制(https://stackoverflow.com/questions/2534260/var-undefined-true) –

+0

@ nem035将无法正常工作 –

+1

*“有什么办法来防止在javascript的本地作用域中以'undefined'关键字命名的变量的声明?“*为什么你确切地问?你的最终目标是什么?你想阻止自己创建这样的变量吗?如果不是你自己,还有谁在“在本地范围内”编写代码? –

回答

3

要解决undefined的意外修改问题,您不应该在代码中编写这个危险的词。

既然你只需要读取访问undefined,建议总是使用void 0相反,它返回undefined

var person = { 
 
    method: function() { 
 

 
     var undefined = 2 
 

 
     if(void 0) { 
 
      console.log("this should never be printed") 
 
     } else { 
 
      console.log("void 0 is always undefined, no matter what you do") 
 
     } 
 

 
    } 
 
} 
 

 
person.method()

如何使用void 0工作,彻底摆脱单词 “未定义” 的?

// Turn 
if(myVariable === undefined) { ... } 
if(typeof myVariable === "undefined") { ... } 
// into 
if(myVariable === void 0) { ... } 

// Turn 
if((myVariable === undefined) || (myVariable === null)) { ... } 
// into 
if(myVariable == null) { ... } 

// Turn 
myObject.someKey = undefined 
// into 
myObject.someKey = void 0 
+0

但是,if(void 0){'真的没有用,因为从来没有人写过'if(undefined){'。更可能的是某些东西需要针对'undefined'进行测试,例如:'if(x === undefined){'。这将如何与'void 0'一起工作? –

+0

'if(x === void 0){'(see my answer) – ideaboxer

+0

为什么不只是'if(x){}'? –

2

欢迎来到精彩的JavaScript世界!

有没有办法阻止某人这样做,但有一种方法可以确保undefined意味着undefined如果你设置你的功能如下(而不是你真的应该这样做,因为它会很对于任何人来说,不正确的做法是设置一个名为undefined的变量)。基本上较小的作用域函数可以隐藏较高范围的undefined变量。

// This is just a way to get a variable called "undefined" 
 
function wrapper(){ 
 
    var undefined = 10; 
 
    console.log("wrapper says undefined is: " + undefined); 
 

 
    // This function declared a parameter called "undefined", 
 
    // but if the function gets called with no argument, then 
 
    // the value of this, more local, argument will truly be 
 
    // undefined. If arguments are needed, just add "undefined" 
 
    // to the end of the list. 
 
    function foo(undefined){ 
 
    // Formal test to ensure that undefined is what we believe it to be: 
 
    if(typeof undefined === "undefined"){ 
 
     console.log("foo says undefined is: " + undefined); 
 
    } 
 
    } 
 
    
 
    // When foo is called with no arguments passed in, the parameter 
 
    // "undefined" will take on a value of undefined within the scope 
 
    // of that function. 
 
    foo(); 
 
} 
 

 
wrapper();

现在,这是一个有点做作,你不打算建立所有的功能与“假”的说法,但你可以测试一下,看看是否undefined已经改变与:

function wrapper(){ 
 
    var undefined = 10; 
 
    console.log(undefined); 
 
    
 
    function foo(){ 
 
    
 
    if(typeof undefined === "undefined"){ 
 
     // undefined has not been altered 
 
    } else { 
 
     // undefined has been altered, so reset it for this scope: 
 
     let undefined; 
 
     console.log(undefined); 
 
    } 
 
    
 
    } 
 
    
 
    foo(); 
 
} 
 

 
wrapper();

在端虽然,您可以通过在您的函数中添加var undefined来简单地防止影响您的功能。无论您在哪里声明,提升都将确保这个功能在您的功能的顶部起作用。

1

我看不到任何方法来阻止它,但是,是的,你可以限制它通过里面的是如果你可以使范围本地使用ES6。

你会发现现在如何改变范围,这不是一回事。

var person = { 
    method : function(){ 

    let undefined = 2 

    if(undefined){ 
     console.log("undefined works as a variable") 
    } 

    } 
}