简体   繁体   English

javascript中的name关键字是什么?

[英]what is name keyword in javascript means?

I am a beginner in javascript , I was trying to play with variables until i reached to this issue, when i have a variable called name , and i commented its definition step , it still has the same value , although i commented it . 我是javascript的初学者,在达到这个问题之前,我一直在尝试使用变量,当我有一个名为name的变量,并且在其定义步骤中进行了注释时,尽管它已进行了注释,但它仍然具有相同的值。 and this happens only with keyword "name" , so what is it ? 而且只有关键字“名称”会发生这种情况,那是什么?

<script>
   // var name="mina"; i commented it now , so if you want to test , enable it once , and then comment it , it will still give you the result although i commented it and deleted the cache
  //  var name;
alert(name);
</script>

name is not a reserved word in Javascript. name不是Javascript中的保留字。 As the sample code is not executing within any specific scope (within a function ect.) the name variable is referencing window.name . 由于示例代码未在任何特定范围内(在函数等范围内)执行,因此name变量引用window.name

console.log(name);
// output: "" (window.name)

(function(){
 console.log(name);
})()
// output: undefined

See https://developer.mozilla.org/en/docs/Web/API/Window/name for more details. 有关更多详细信息,请参见https://developer.mozilla.org/en/docs/Web/API/Window/name

The variable is not in a function, so it's a global variable. 变量不在函数中,因此它是全局变量。 Global variables are added to the window and it stays there (until you refresh the page if I recall correctly). 全局变量被添加到窗口中,并停留在该窗口中(如果我没有记错的话,直到刷新页面为止)。

You don't reset the variable so it will keep having the same value. 您无需重置变量,因此它将保持相同的值。

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

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