JavaScript 使用定时器传入“2010-10-15”的参数 被内部eval
今天遇到一个有点小郁闷的问题。看代码:
<html><head><script>function b(id, id2){ alert(id + "---->" + id2);}function a(){ var c = '123'; var s = "2005-12-15"; window.setInterval("b(" + c + "," + s + ")", 100);}a();</script></head><html>
弹出:
http://dl.iteye.com/upload/attachment/272169/5cfdd91d-5c41-32eb-aa71-d1f26df61c33.png
我想应该不对啊,所以就做了一下的修改!
<html><head><script>function b(id, id2){ alert(id + "---->" + id2);}function a(){ var c = '123'; var s ="2005-12-15"; window.setInterval(b(c, s), 100);}a();</script></head><html>
嘿嘿 可以了!但不明白为什么。
http://dl.iteye.com/upload/attachment/272173/b0056437-8151-3f80-be99-40e8205c1f58.png
最后我想是不是
按照我那个写法,出来就是1978
在内部被eval("2005-12-15") ,掉了
“-” 这个符号被当作减号了
果然是这个问题。那就是闭包的问题了
所以我又修改了一下
<html><head><script>ffunction b(id, id2){ alert(id + "-->" + id2);}function a(){ var c = '123'; var s = '2005-12-15'; window.setInterval("b('"+c+"','"+s+"')", 100);}a();</script></head><html>
嘿嘿 弹出结果为:
http://dl.iteye.com/upload/attachment/272173/b0056437-8151-3f80-be99-40e8205c1f58.png
呵呵 解决。
不过 对于 JavaScript里面的闭包问题 我还是不是很明白 所以有待以后再去深入了。
页:
[1]