为什么JavaScript函数总是返回一个值? [英] Why JavaScript functions always return a value?

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

问题描述

我正在学习JavaScript编程课程,教师说典型的JavaScript函数总是返回一个值。即使我们没有提供任何明确的返回值,引擎也会返回 undefined

I'm taking a course in JavaScript programming, and the instructor said that a typical JavaScript function always returns a value. Even when we don't provide any explicit return value, the engines return undefined.

这是真的吗?如果是这样,为什么?

Is that true? If so, why?

推荐答案

这是真的 - 因为这就是JavaScript的设计方式。

It's true—because that's how JavaScript was designed.

但我认为这不是你想要的答案,所以让我们考虑一下......

试着让自己置身于 Brendan Eich ,设计JavaScript的人!

But I don't think that's the answer you were looking for, so let's think about it...
Try to put yourself in the shoes of Brendan Eich, the person who designed JavaScript!

静态语言中,一个没有返回任何内容的函数之间通常有区别( void function),以及返回某个值的函数。 Brendan选择设计一种动态语言,即一种不需要您定义函数返回类型的语言。因此,JavaScript 不会检查您从函数中返回的内容,从而为您提供完全的自由。

In static languages, there is usually a distinction between a function that doesn't return anything (void function), and a function that returns some value. Brendan chose to design a dynamic language, that is, a language that doesn't require you to define function return types. So JavaScript doesn't check what you return from the function, giving you full freedom.

您可以拥有一个返回数字的函数。 ..

You can have a function that returns a number...

function computeSomething() {
  return 2;
}

...或字符串......

... or a string ...

function computeSomething() {
  return 'hi';
}

......或者,事实上,其中任何一个:

... or, in fact, any of them:

function computeSomething() {
  if (Math.random() > 0.5) {
    return 2;
  } else {
    return 'hello';
  }
}

有时你不需要计算任何东西 - 你只需要一些东西。

所以你不要退货。

Sometimes you don't need to compute anything—you only need to do something.
So you don't return anything.

function doSomething() {
   console.log('doing something');
}

但是,我们可能想要在中间退出一个函数,并且由于 return< value> 已经完全,因此允许写返回没有值来支持这个用例:

We may, however, want to exit a function in the middle of it, and since return <value> already does exactly that, it makes sense to allow writing return without a value to support this use case:

function doSomething(num) {
   if (num === 42) {
     return;
   }

   while (true) {
     doSomethingElse();
   }
}

这也与C / Java语法一致,是确保JavaScript采用的目标之一。

This is also consistent with C/Java syntax, which was one of the goals to ensure JavaScript adoption.

是的,有一个问题:如果我们放一个简单的返回进入一个应该计算某事的函数?请注意,我们不能取缔这个:我们之前的一个决定是将JavaScript变为动态语言,我们不会检查函数返回的内容。

Aye, there's the rub: what happens if we put a plain return into a function supposed to compute something? Note that we can't outlaw this: one of our earlier decisions was to make JavaScript a dynamic language, where we don't check what the function returns.

function computeSomething(num) {
  if (num === 42) {
    return; // just return? o_O
  }

  if (Math.random() > 0.5) {
    return 2;
  } else {
    return 'hello';
  }
}

var x = computeSomething(2); // might be 2, might be 'hello'
var y = computeSomething(42); // ???

当然Brendan本可以决定在这种情况下提出错误,但他明智地决定不这样做,因为它会导致难以发现的错误和太容易破解的代码。

Of course Brendan could have decided to raise an error in this case, but he wisely decided not to, because it would lead to hard-to-find errors and too easily breakable code.

所以一个空的返回有一个含义return undefined

So an empty return got a meaning "return undefined".

但是早期或最后返回的函数有什么区别?从调用代码的角度来看,应该没有任何东西。当确切地返回函数时,调用代码不应该知道;它只对返回值(如果有的话)感兴趣。

But what's the difference between the function returning early, or at its end? There shouldn't be any, from the calling code's point of view. Calling code is not supposed to know when exactly the function returned; it is only interested in return value (if any).

因此唯一合乎逻辑的结论是使未定义如果函数未通过显式 return< value> 运算符指定一个,则为默认返回值。因此,返回并且函数执行到它的结束语义匹配。

The only logical conclusion thus would be to make undefined the "default" return value if function does not specify one via explicit return <value> operator. Thus, return and function-executed-to-its-end semantics match.

Python,另一种动态语言在JavaScript之前出现,以同样的方式解决了这个问题:如果函数 没有指定返回值

Python, another dynamic language that came before JavaScript, solves this problem in the same way: None is returned if function doesn't specify return value.

这篇关于为什么JavaScript函数总是返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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