函数应始终返回某些内容(JavaScript) [英] Should functions always return something (Javascript)

查看:50
本文介绍了函数应始终返回某些内容(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数应该总是返回一些东西吗?我经常写一些非常基本的函数,用作一些经常发生的事情的速记,例如:

Should functions always return something? I often write very basic functions which are used as shorthand to do things that happen a lot such as:

function formsAway() {
    $("#login_form, #booking_form").slideUp();
}

该功能应该存在吗?是否有更好的方法?还是这样?

Should this function exist - is there a better way, or is this fine?

推荐答案

他们不必返回任何内容.如果将其保留为空白,则仅返回"undefined",在这种情况下就可以了,因为您从不打算使用返回值.Javascript语法非常简单,据我所知,有功能的函数和没有返回值的函数(除了'return'关键字)之间没有任何真正的区别

They don't have to return anything. If you leave it blank it simply returns 'undefined' which in this case is fine because you never intend to use the return value. The Javascript syntax is pretty simplistic and as far as I know there just isn't any real distinction between functions that do and functions that don't return a value (other than the 'return' keyword)

这篇关于函数应始终返回某些内容(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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