Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an intermediate value?

Tags:

Today I bumped exactly into this issue: 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).

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:

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 mdn I found this page that talks about Array.from, where the concept of "intermediate array" can be found:

More clearly, Array.from(obj, mapFn, thisArg) has the same result as Array.from(obj).map(mapFn, thisArg), except that it does not create an intermediate array.

But besides pieces of info here and there, it's not clear what an intermediate value is.

Is there an official definition to this?

like image 436
Adelin Avatar asked May 14 '18 11:05

Adelin


People also ask

What do you mean by intermediate value?

Intermediate value theorem states that if “f” be a continuous function over a closed interval [a, b] with its domain having values f(a) and f(b) at the endpoints of the interval, then the function takes any value between the values f(a) and f(b) at a point inside the interval.

What is the intermediate value theorem in simple terms?

Explanation: The intermediate value theorem states that if f(x) is a Real valued function that is continuous on an interval [a,b] and y is a value between f(a) and f(b) then there is some x∈[a,b] such that f(x)=y .

What does it mean for a function to have the intermediate value property?

A function f:A→E∗ is said to have the intermediate value property, or Darboux property, 1 on a set B⊆A iff, together with any two function values f(p) and f(p1)(p,p1∈B), it also takes all intermediate values between f(p) and f(p1) at some points of B.


1 Answers

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) + d the result of b * c is an intermediate value in the right-hand-side expression.

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.)

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 evaluation but such values cannot be stored as properties of objects or values of ECMAScript language variables.

(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:

"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);  }
like image 127
T.J. Crowder Avatar answered Sep 26 '22 09:09

T.J. Crowder