有和没有return语句的函数之间有区别吗? [英] Is there a difference between a function with and without a return statement?

查看:131
本文介绍了有和没有return语句的函数之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有2个相同的函数没有返回值

Say you have 2 identical functions that do not return a value

function a() {
    // do some interesting things
}

function b() {
    // do the same interesting things
    return;
}

功能 b 是显然更冗长,但这些之间是否有任何功能差异?

Function b is obviously more verbose but is there any functional difference between these?

推荐答案

没有真正的区别;两者都将返回 undefined

There's no real difference; both will return undefined.

没有return语句的函数将返回 undefined ,以及带有空的函数返回声明。

Functions with no return statement will return undefined, as will functions with an empty return statement.

要自己确认,可以运行此代码 - FIDDLE

To confirm this for yourself, you can run this code -- FIDDLE:

​function a() {
}

function b() {
    return;
}

var aResult = a();
var bResult = b();

alert(aResult === bResult);  //alerts true

这篇关于有和没有return语句的函数之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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