javascript:匿名函数和声明函数有什么区别?

时间:2014-03-16 16:06:58

标签: javascript

这是jsFiddle:http://jsfiddle.net/CGAb7/

$( ".tweet1" ).click(function() {
    alert( "this is tweet1" );
});  

$('.tweet2').click(tryAlert());
  function tryAlert(){
    alert('this is tweet2');
  }

为什么第二个版本(声明的函数)会自动运行?

1 个答案:

答案 0 :(得分:5)

为了将函数作为参数传递,在您的情况下省略()

$('.tweet2').click(tryAlert);

这允许在需要时调用该函数。添加()会立即调用该函数。

相关问题