这两个函数/方法有什么区别? [英] What is the difference between these two functions/approaches?
问题描述
我只使用jQuery编写JavaScript代码。有一件令我困惑的事情是这两种写作方法,
第一种方法
< pre $ lt; code> vote = function(action,feedbackId,responseDiv)
{
alert('hi');
返回feedbackId;
}
第二种方法
function vote(action,feedbackId,responseDiv)
{
alert('hi');
返回feedbackId;
$ / code>
两者之间有什么区别,为什么要使用第一种方法或第二种方法是什么?
函数表达式 > vote
变量,第二个是函数声明。主要区别在于函数语句在解析时间,它们在运行时声明之前是可用的。
另见:
I use only jQuery for writing JavaScript code. One thing that confuses me is these two approaches of writing functions,
First approach
vote = function (action,feedbackId,responseDiv)
{
alert('hi');
return feedbackId;
}
Second approach
function vote(action, feedbackId,responseDiv)
{
alert('hi');
return feedbackId;
}
What is the difference between the two and why should one use the first approach or the second approach?
The first is a function expression assigned to the vote
variable, the second is a function declaration.
The main difference is that function statements are evaluated at parse time, they are available before its declaration at runtime.
See also:
- Named function expressions demystified (article)
- Explain JavaScript’s encapsulated anonymous function syntax
这篇关于这两个函数/方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!