getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.98k stars 33.46k forks source link

Scope and Closures, Chapter 1 - confusing wording #1548

Closed ballwood closed 4 years ago

ballwood commented 4 years ago

The following seems confusing to me (link)

JS functions are themselves values, meaning they can be assigned and passed around just like numbers or strings.

Comparing functions to numbers/string and saying they are “values” would mean they are a value type and a new copy is made when they are passed around which isn’t the case

I’m no writer but maybe the following is better?

Defining a JS function returns a reference to the function. This means they can be assigned and passed around just like objects or arrays.

getify commented 4 years ago

saying they are “values” would mean they are a value type

Eh, I don't agree with that assumption. The notion of a value is to identify a piece of data that we can use, manipulate, assign, and inspect during the run-time of a program, as opposed to the program itself. It doesn't mean anything more specific than that.

In JS we distinguish between primitive values (numbers, strings, etc) and object values (objects, arrays, functions) based on whether these values are held directly or by reference, and whether assignments are value copies (primitive values) or reference copies (object values). But in all those cases, we still think of them as values.

The notion of identifying a function as a value has strong precedence outside of (and pre-dating) JS, as opposed to languages which do not treat functions as a value in any way -- in those languages, closure cannot exist, because functions are just static parts of the program and cannot be accessed by the program during its run-time.

Your distinction (about functions being held by reference and assigned by reference-copy) is not inaccurate, but it's also not the point of the sentence. I don't think I want to mention value-vs-reference semantics here, as it complicates the discussion.

ballwood commented 4 years ago

I don’t feel that something that small a change is really bringing a level of complexity to the discussion. It’s just tightening the definition so there can be no ambiguity.