javascript 什么是中间值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50329128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an intermediate value?
提问by Adelin
Today I bumped exactly into this issue: Uncaught TypeError: (intermediate value)(...) is not a function
今天我正好碰到了这个问题:Uncaught TypeError: (intermediate value)(...) is not a function
So yeah, after placing the semicolon where appropriate, it no longer throws that error. However, I never knew there is such a concept in JavaScript (intermediate value).
所以是的,在适当的地方放置分号后,它不再抛出该错误。但是,我从来不知道 JavaScript 中有这样的概念(intermediate value)。
Apparently you can generate a similar variation of that error with this piece of code:
显然,您可以使用这段代码生成该错误的类似变体:
[myFunc] = function(someVar){
console.log(someVar);
return 7;
}();
//error thrown: (intermediate value) is not a function or its return value is not iterable
And if you name the function, it's no longer intermediate:
如果你给函数命名,它就不再是intermediate:
function hi(){return undefined}
[a] = hi();
// error thrown: hi is not a function or its return value is not iterable
I understand that it refers to something that is intermediate, but in this case we have an anonymous function, and there are ways to determine if a function is anonymous, so the error message could've been a little more explicit.
我知道它指的是中间的东西,但在这种情况下,我们有一个匿名函数,并且有办法确定一个函数是否是匿名的,因此错误消息可能会更明确一些。
Searching the js mozilla mdnI found this pagethat talks about Array.from, where the concept of "intermediate array" can be found:
搜索的JS Mozilla的MDN我发现这个网页是关于会谈Array.from,其中“中间阵”的概念,可以发现:
More clearly,
Array.from(obj, mapFn, thisArg)has the same result asArray.from(obj).map(mapFn, thisArg), except that it does not create an intermediate array.
更清楚的是,与
Array.from(obj, mapFn, thisArg)具有相同的结果Array.from(obj).map(mapFn, thisArg),只是它不创建中间数组。
But besides pieces of info here and there, it's not clear what an intermediate value is.
但是除了这里和那里的信息之外,还不清楚中间值是什么。
Is there an official definition to this?
这个有官方定义吗?
采纳答案by T.J. Crowder
An "intermediate value" is just a value that's produced inside an expression that isn't the final value of the expression. In a = (b * c) + dthe result of b * cis an intermediate value in the right-hand-side expression.
“中间值”只是在表达式中生成的值,而不是表达式的最终值。在a = (b * c) + d的结果b * c是在右手侧表达式的中间值。
Yes, in this specific case, the error message could say "(anonymous function) is not a function or its return value is not iterable". Not all intermediate values are anonymous functions, though. The implementers of V8 just chose to use a generic error message. (SpiderMonkey [in Firefox] uses the same terminology, though a different message.)
是的,在这种特定情况下,错误消息可能会说“(匿名函数)不是函数或其返回值不可迭代”。不过,并非所有中间值都是匿名函数。V8 的实现者只是选择使用通用错误消息。(SpiderMonkey [在 Firefox 中] 使用相同的术语,但信息不同。)
Is there an official definition to this?
这个有官方定义吗?
The specification uses the term "intermediate result" here, to mean essentially the same thing as "intermediate value" (to my eye):
该规范在此处使用术语“中间结果” ,其含义与“中间值”基本相同(在我看来):
6.2 ECMAScript Specification Types
A specification type corresponds to meta-values that are used within algorithms to describe the semantics of ECMAScript language constructs and ECMAScript language types. The specification types include Reference, List, Completion, Property Descriptor, Lexical Environment, Environment Record, and Data Block. Specification type values are specification artefacts that do not necessarily correspond to any specific entity within an ECMAScript implementation. Specification type values may be used to describe intermediate results of ECMAScript expression evaluationbut such values cannot be stored as properties of objects or values of ECMAScript language variables.
6.2 ECMAScript 规范类型
规范类型对应于在算法中使用的元值,用于描述 ECMAScript 语言构造和 ECMAScript 语言类型的语义。规范类型包括引用、列表、完成、属性描述符、词法环境、环境记录和数据块。规范类型值是规范人工制品,不一定对应于 ECMAScript 实现中的任何特定实体。规范类型值可用于描述 ECMAScript 表达式评估的中间结果,但此类值不能存储为对象的属性或 ECMAScript 语言变量的值。
(my emphasis)
(我的重点)
Note that your code samples are not identical other than giving the function a name. One of them attempts to iterate the value 7. The other attempts to iterate the value undefined. It doesn't matter to the error message from V8 but it did for SpiderMonkey. Let's compare apples with apples, remove irrelevancies, and declare our variable:
请注意,除了为函数命名之外,您的代码示例并不相同。其中之一尝试迭代 value 7。另一个尝试迭代值undefined。来自 V8 的错误消息无关紧要,但对于 SpiderMonkey 却如此。让我们比较一下 apples 和 apples,去掉不相关的部分,然后声明我们的变量:
"use strict";
var myFunc;
try {
[myFunc] = function(){
return 7;
}();
} catch (e) {
console.error(e.message);
}
try {
[myFunc] = function hi(){
return 7;
}();
} catch (e) {
console.error(e.message);
}
try {
function hi(){
return 7;
}
[myFunc] = hi();
} catch (e) {
console.error(e.message);
}

