Javascript变量评估

时间:2012-08-01 09:57:42

标签: javascript variables

(function(){

   var commentList = $("#commentList");

});

鉴于上述情况,每次使用变量时都会评估commentList吗?

4 个答案:

答案 0 :(得分:2)

不,该变量会存储对它的引用,因此每次使用commentList时,您都不会重新评估$("#commentList")(当然,第一项作业除外)

答案 1 :(得分:2)

每次调用函数时都会重新进行评估。

一旦进入函数,它将被评估一次,而不是每次调用var。

答案 2 :(得分:2)

没有。你可以轻松检查它

<script>
$(function(){

   var commentList = $("#commentList");
   console.log(commentList);
   $('#commentList').html('');
   console.log(commentList);

});
</script>

<div id="commentList">Test</div>

答案 3 :(得分:2)

当作业发生时,它会被评估一次。

演示:http://jsfiddle.net/PxRXF/