在ECMAScript中返回并返回undefined

时间:2018-04-20 21:02:31

标签: javascript ecmascript-6

当我阅读规范时,我注意到有几种返回。

其中一个是return undefined。可以理解该记录,因为ECMAScript算法使用undefined的值。但是return没有争论意味着什么呢?在规范中,我没有看到这种行为的描述。

例如:http://www.ecma-international.org/ecma-262/#sec-putvalue部分6. d. Return.

4 个答案:

答案 0 :(得分:1)

The semantics of undefined are straightforward: it's the language telling you, "I have no idea". So for instance if you do this:

const a = [17]
a[3] = 42

And then try to fetch the value at index 1:

a[1]

you will get undefined. Think of the system as saying, "Well, you started with a one-element array, and then set the value at index 3, which I know means that you now have length 4, so I can go look at element with index 1. Oh dear, that hasn't been set. So I'll just tell you that it's undefined."

There is nothing to enforce that your function have a return statement1, or that, if it does, you have to supply an actual value. So if you call a function and capture its return value, there is no guarantee that there is something there to capture.

In this scenario:

function foo(x) {console.log(x);}
const result = foo(42);

the value of result will be undefined. We didn't return anything. But the same thing is true of this function:

function bar(x) {console.log(x); return;}

And in this one:

function baz(x) {console.log(x); return undefined;}

we explicitly return the undefined value.

We could even do this:

function qux(x) {return console.log(x);}

and because console.log itself returns undefined, so will our function.

These all have exactly the same behavior from the perspective of a user. I suppose there is some philosophical question about how the undefined we receive was generated, but there are no practical difference.


1 This is in contrast to various other languages. Some require an explicit return, some simply return the value of the last expression evaluated in the function. Still others distinguish functions, which must have a return from callable procedures, which don't. Some have a signal value, something like undefined for when nothing is returned, usually nil or null. But Javascript's null semantics are a bit different from that.

答案 1 :(得分:1)

之前在Implicit Completion Values部分的规范中解释了这种用法:

  

在算法步骤中没有值的“return”语句意味着与:

相同      
      
  1. 返回NormalCompletion未定义)。
  2.   

因此,只要规范使用不带值的Return语句,就可以假定它返回undefined。

答案 2 :(得分:1)

  

对于没有参数的Return,这意味着什么,例如在步骤6中。 PutValue

它在规范中主要是自然语言,所以"返回"只是意味着"中止这个算法,不再运行任何进一步的步骤"。如果你想对你的形式更加彻底:

"Algorithm Conventions" chapter知道&#34; 调用抽象操作返回完成记录。&#34;和"Implicit Completion Values" chapter州&#34; < em>在算法步骤中没有值的“返回”语句意味着同样的事情:
返回NormalCompletion(undefined)。&#34;

好吧,它们是隐式完成值。

答案 3 :(得分:0)

<强>前言

由于您似乎对JavaScript与ECMAScript之间的关系感到困惑,让我分享一下 from Wikipedia on JavaScript

  

JavaScript(/dʒɑːvəˌskrɪpt/), [6]通常缩写为JS,是一个   高级的,解释性的编程语言。这是一种语言   也被称为动态,弱类型,基于原型和   多范式。

     

除了HTML和CSS,JavaScript是三个核心之一   万维网的技术。[引证需要]它已经习惯了   使动态网页互动并提供在线程序,   包括视频游戏。大多数网站都使用它[引用   需要],并且所有现代网络浏览器都支持它而不需要   插件通过内置的JavaScript引擎。每一个   JavaScript引擎代表JavaScript的不同实现,   全部基于ECMAScript规范,而某些引擎则没有   完全支持该规范,并与许多引擎支持额外的   ECMA以外的功能。

因此,虽然某些浏览器可能不完全支持最新的规范(即Chrome 10不符合ES2017)。还有一些支持附加功能,任何符合“标准”的客户端(即所有现代主流客户端)都有一个处理ECMAScript的运行时。

简而言之,当您在符合标准的客户端中编写JavaScript时,您正在编写ECMAScript。

只有一种return。如果您只是return来自函数而未指定值,则函数返回undefined。如果你确实指定了一个值,那就是返回的值。

实施例

function returnUndefined(){
  return; // Notice we're not specifying anything to return
}

console.log(returnUndefined()); // undefined

function returnUndefined2(){
  // Here, we are explicitly returning undefined, although no one actually does this
  // because if that's what you want, just use plain old return
  return undefined; 
}

console.log(returnUndefined2()); // undefined

function returnSomething(){
  return "Summer"; // Here, we are explicitly returning a meaningful value
}

console.log(returnSomething()); // "Summer"