因为saysAlert
是一个函数,它仅在您返回saysAlert
哪所房子时function
以及在您声明alice
.
这会给你一个未定义的变量:
<script type="text/javascript">
function sayAlice() {
var sayAlert='';
console.log(alice); // alice is undefine up to this line
var alice = 'Hello Alice';
return sayAlert;
}
sayAlice()();
</script>
您的原始代码:
<script type="text/javascript">
function sayAlice() {
var sayAlert = function() { console.log(alice); } // function sayAlert is not yet implemented
var alice = 'Hello Alice';
return sayAlert; // function alert is implemented after the alice variable is define
}
sayAlice()();
</script>