0

为什么以下行没有提醒?

  <script>
  alert(x);//this line is not executed or alerted  ???.It should have alerted "undefined" 
  console.log(x)//reference error 
  var y="maizere";
  alert(y);//this line is also not executed or alerted  ???

  </script>

任何未声明的变量都被视为 javascript 中的全局变量,对吗?

4

3 回答 3

2
alert(x);//this line is not executed or alerted  ???.It should have alerted "undefined" 

这是不正确的。这条线引发了 aReferenceError因为没有 variable x

将此与情况进行对比:

var obj = {};
alert(obj.x); // undefined - there's no attribute x
于 2013-01-31T14:38:17.127 回答
2

你越来越undefined困惑undeclared

该变量x未声明 - 它尚未在代码中声明,因此

alert(x);

会引发类似的错误'x' is not declared

您的代码应阅读

<script>
  var x;
  alert(x);//this line is not executed or alerted  ???.It should have alerted "undefined" 
  var y="maizere";
  alert(y);//this line is also not executed or alerted  ???
</script>
于 2013-01-31T14:41:35.663 回答
0

alert应该定义您在调用中放入的任何内容。如果不是(您的情况),则会引发错误。因此,您应该首先定义变量。对于错误,您应该检查 JavaScript(浏览器)控制台。

var x = "Test";
alert(x); // shows
var y = 123;
alert(y); // y also shows, because there is no error thrown inside the alert for x
于 2013-01-31T14:38:01.967 回答