嵌套函数结构使setInterval函数不可见
Nested function structure make setInterval function unvisible
代码 1
<html>
<head>
<script type="text/javascript">
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
function startTime(){
setInterval("showTime()",1000);
}
</script>
</head>
<body onload="startTime()">
<div id="text"></div>
</body>
</html>
运行成功,现在在startTime函数中嵌套showTime函数
code2
<html>
<head>
<script type="text/javascript">
function startTime(){
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
setInterval("showTime()",1000);
}
</script>
</head>
<body onload="startTime()">
<div id="text"></div>
</body>
</html>
发生错误,ReferenceError: showTime is not defined
。
1.For code1, setInterval 函数中双引号的用法是什么?
2.For code2,为什么showTime可以像code1那样从startTime开始调用?
"showTime()"
是一个不可调用的字符串。
正确的方法是:
function startTime(){
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
setInterval(showTime,1000);
}
我认为这可能是评估范围问题。
function test() {
var x = 2,
y = 4;
console.log(eval("x + y")); // Direct call, uses local scope, result is 6
var geval = eval;
console.log(geval("x + y")); // Indirect call, uses global scope, throws ReferenceError because `x` is undefined
}
test();
内部setInterval也使用eval判断函数。
因此,由于直接和间接调用的这种行为,它将引发引用错误。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
代码 1
<html>
<head>
<script type="text/javascript">
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
function startTime(){
setInterval("showTime()",1000);
}
</script>
</head>
<body onload="startTime()">
<div id="text"></div>
</body>
</html>
运行成功,现在在startTime函数中嵌套showTime函数
code2
<html>
<head>
<script type="text/javascript">
function startTime(){
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
setInterval("showTime()",1000);
}
</script>
</head>
<body onload="startTime()">
<div id="text"></div>
</body>
</html>
发生错误,ReferenceError: showTime is not defined
。
1.For code1, setInterval 函数中双引号的用法是什么?
2.For code2,为什么showTime可以像code1那样从startTime开始调用?
"showTime()"
是一个不可调用的字符串。
正确的方法是:
function startTime(){
function showTime(){
var c=document.getElementById("text");
var nowTime=new Date();
c.innerHTML="time is "+nowTime.toLocaleTimeString();
}
setInterval(showTime,1000);
}
我认为这可能是评估范围问题。
function test() {
var x = 2,
y = 4;
console.log(eval("x + y")); // Direct call, uses local scope, result is 6
var geval = eval;
console.log(geval("x + y")); // Indirect call, uses global scope, throws ReferenceError because `x` is undefined
}
test();
内部setInterval也使用eval判断函数。 因此,由于直接和间接调用的这种行为,它将引发引用错误。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval