这两个函数/方法有什么区别? [英] What is the difference between these two functions/approaches?

查看:91
本文介绍了这两个函数/方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只使用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:

这篇关于这两个函数/方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆