News for hackers. Stuff that matters.
Re:JavaScript for Impatient Programmers by eurasiantiger on Wednesday July 01, 2020 @06:51AM (#23700058)
I would not suggest this book. While it does begin with simple, concise examples, it leaves out many important details.

For example:

- The first variable using `const` is called ”immutable”, which is misleading, since only the _assignment_ is immutable. Later on the same page, a const object property is assigned to without any clarification as to why it works.

- Only single- and double-quoted strings are introduced at first, even though template strings are arguably the cleanest for building strings.

- Arrow functions are introduced as just another syntax - neither implicit `this` binding nor implicit return of last block statement are mentioned.

Re:JavaScript for Impatient Programmers by rauschma on Wednesday July 01, 2020 @08:05AM (#23700483)
> The first variable using `const` is called ”immutable”, which is misleading, since only the _assignment_ is immutable

That’s a first look at the syntax. The details are explained here: https://exploringjs.com/impatient-js/ch_variables-assignment...

But I should clarify that “immutable” here means “immutable binding”.

> Only single- and double-quoted strings are introduced at first

I had to start somewhere. Template literals and tagged templates are a more complex topic that benefits from strings already having been introduced.

> Arrow functions are introduced as just another syntax

Arrow functions are introduced in detail here: https://exploringjs.com/impatient-js/ch_callables.html

---

In general, I occasionally have to initially leave out some details so that the book can be read linearly. But those details are then filled in later in the book.

Re:JavaScript for Impatient Programmers by gabrielsroka on Wednesday July 01, 2020 @10:31AM (#23701736)
Thank you for your books and websites! After reading 1,000 sites explaining that async/await is just "syntactic sugar" [hint, it's not], you're the only one who explains it correctly.
Re:JavaScript for Impatient Programmers by throwanem on Wednesday July 01, 2020 @10:21AM (#23701611)
You should define and describe the concept of "binding", if you don't already. Telling me a const declaration is an immutable binding does nothing for me if I don't already know, or immediately find out, that binding is the general case of assignment, an association of a value with a name. Given that and an understanding of the differences between value and reference types, there are few surprises left for me in how Javascript handles names and values.

I sometimes find myself informally teaching devs less experienced with the language, and I've learned to cover this part of it very early. It's not made terribly obvious in a lot of resources people have mentioned using, and I've yet to encounter a case where developing that understanding failed to help someone better understand the code they were writing, and thus write better code. (Usually I'm also reviewing a lot of their PRs, and that makes the change pretty easy to see.)

Re:JavaScript for Impatient Programmers by rauschma on Wednesday July 01, 2020 @10:51AM (#23701980)
I agree! I’m in the process of editing the relevant chapter.
Re:JavaScript for Impatient Programmers by throwanem on Wednesday July 01, 2020 @11:12AM (#23702287)
Excellent!

I've been looking a long time for a good "Javascript in a Box" that I can give to mentees as a tool for extended learning beyond what we can cover directly. Your book and its associated materials look as if they might be just the thing, and I'm evaluating them as such.

Is there an email list or something I can use to be notified when the revisions you describe have landed? I have someone already in mind to be a "beta learner", but I'd rather wait a little for the updated version, and it would help to get some kind of push when it's available.

Re:JavaScript for Impatient Programmers by rauschma on Wednesday July 01, 2020 @11:23AM (#23702453)
Ping me at dr_axel AT icloud.com and I’ll notify you. Two universities in Switzerland are using my book to teach JavaScript and seem happy with it, so far.
Re:JavaScript for Impatient Programmers by Stratoscope on Wednesday July 01, 2020 @02:39PM (#23704887)
> an understanding of the differences between value and reference types

It's a common misconception among JavaScript programmers, especially experienced ones, that such a difference exists. It doesn't.

The usual explanation is that there are value types such as number or string, and reference types such as array or object, and these are treated differently both by the assignment operator and when passing an argument into a function: the value is copied for a value type, but a reference is copied for a reference type.

This is often illustrated with an example like this:

  function a( str ) {
      str = 'moo';
  }
  
  let s = 'bar';
  console.log( s );  // 'bar'
  a( s );
  console.log( s );  // still 'bar', because a string is a value type
  
  function b( obj ) {
    obj.foo = 'moo';
  }
  
  let thing = { foo: 'bar' };
  console.log( thing.foo );  // 'bar'
  b( thing );
  console.log( thing.foo );  // now 'moo', because an object is a reference type
This code does work as the comments describe: 'thing.foo' changes but 'n' doesn't. But this is not because of any distinction between "value types" and "reference types".

It's because the code in the a() and b() functions is different!

The code in a() reassigns the function parameter (which is a local variable) to a new value. The code in b() doesn't do this, it sets a property of its parameter.

The difference in behavior is solely due to this difference in code.

Every example that purports to illustrate how "value types" and "reference types" are handled differently in assignment or parameter passing makes this same mistake.

To illustrate further, strings are commonly described as a "value type", as in the example above, and most of these discussions say that when you assign or pass a value type, the entire value is copied. But no JavaScript engine worth its salt does this. If you have code like this:

  let str = 'one billion characters here';
  let copy = str;  // Does it *copy* the billion characters? Of course not.
Now for a short fixed-length value such as a number, a JavaScript engine may well store and copy the actual value. But this is an internal optimization, not part of the JavaScript language. It is fundamentally unknowable from JavaScript code whether the engine stores the value inside its internal Value object or stores a reference.

The real difference between so-called value and reference types is that the latter may have mutable properties and the former don't.

One last illustration: if you have an object and then call Object.freeze() on it, the object now has no mutable properties (and none can be added). Does that convert this "reference" type into a "value" type where future assignments will copy the value instead of copying a reference? No. It's just been made immutable.

I will be happy to discuss further if anyone has questions or disagrees.

Re:JavaScript for Impatient Programmers by throwanem on Wednesday July 01, 2020 @04:57PM (#23706582)
This is a splendid example of the kind of thing I let mentees discover on their own, when it actually starts to matter in what they're doing, rather than burden them with right up front. If binding is an uncommonly encountered concept, interning is a practically unheard-of one, and it would be a severe disservice to explain it, and copy-on-write, at the same time as everything else I'm asking someone new to the language, and often new to programming entirely, to follow.

You're right that the description I gave is fundamentally a false one. But it is a harmless, and an extremely useful, lie.

(In any case, you're underplaying your hand here. If you're going to get deep into the weeds on how Javascript values really work, why leave out implicit autoboxing?)

Re:JavaScript for Impatient Programmers by filleduchaos on Thursday July 02, 2020 @02:26AM (#23710552)
I roundly disagree with your logic if not with its conclusion. Your comment makes the (same) mistake of conflating three entirely separate concepts:

- reference/value _data types_ - call by reference/value/value result/etc _evaluation strategies_ - (im)mutability

JavaScript has both reference types and (what are treated in the abstract if not in implementation as) value types. It also implements (solely) a call by value evaluation strategy, which is orthogonal to the previous statement.

Re:JavaScript for Impatient Programmers by zarathustreal on Wednesday July 01, 2020 @07:47AM (#23700389)
> const is called immutable which is misleading

Consider that this may simply be a knowledge gap on your end. I think most programmers understand the concept of value vs reference types and would not assume an object to be a value type. Obviously the reference is immutable, as is any other value type you assign to the const.

> nor the implicit return of last block statement are mentioned

Probably not mentioned because it doesn’t exist. There is no implicit return of the last block statement. That statement is misleading in at least two ways: there cannot be multiple top-level block statements as the body of a function, nor does the singular block statement body of an arrow function implicitly return the last expression. It actually requires an explicit return statement. On the other hand, an arrow function with an expression body -does- implicitly return.

Re:JavaScript for Impatient Programmers by hoorayimhelping on Wednesday July 01, 2020 @09:00AM (#23700832)
>Consider that this may simply be a knowledge gap on your end.

My experience says this is very far from the truth. JavaScript's implementation of const is confusing for almost everyone the first time they come across it, especially the area you're talking about - changing collections. Junior engineers and non JavaScript senior engineers alike don't seem to understand what const actually means until they dig in to get deeper context, which I would point as a pretty big failure of language design.

Re:JavaScript for Impatient Programmers by throwanem on Wednesday July 01, 2020 @10:03AM (#23701410)
I think it's a failure of education, specifically around the concept of binding as the general case of assignment.

I came late to that myself, not encountering it as a discrete concept until I got interested in Lisp, and found it clarified a great deal for me that had previously been obscure.

With that concept available, const is trivially explained as an immutable binding, as indeed the author of the book under discussion explains it elsewhere in this comment thread.

Without it, yeah, const feels full of special cases, just like everything else involving non-primitive types. It's something I've learned to cover early in teaching the language, because a little early effort there goes a long way toward clearing up a lot of confusion later.

Re:JavaScript for Impatient Programmers by maple3142 on Wednesday July 01, 2020 @09:58AM (#23701363)
I don't think it is really confusing if you already know how "final" works in Java or (const) references in C++.
Re:JavaScript for Impatient Programmers by eurasiantiger on Wednesday July 01, 2020 @02:24PM (#23704725)
Good point. However, most web developers starting with JS tend to come from PHP with its &$variable reference-passing magic.

I seem to have chosen my wording badly, since I did not mean (labeled) blocks, but indeed the expression body (which may have been referenced in the book as a ”block”, too lazy to check).

Re:JavaScript for Impatient Programmers by IggleSniggle on Wednesday July 01, 2020 @09:10AM (#23700915)
Maybe they meant implicit return after the last block statement (ie the implicit return void)? But that's not exclusive to arrow functions, so probably not.
Re:JavaScript for Impatient Programmers by mattmanser on Wednesday July 01, 2020 @07:10AM (#23700176)
No book is perfect.
Re:JavaScript for Impatient Programmers by afefers on Wednesday July 01, 2020 @07:02AM (#23700121)
Good points. What would you suggest instead?
Re:JavaScript for Impatient Programmers by lprd on Wednesday July 01, 2020 @07:18AM (#23700221)
My two favorites are YDKJS (You Don't Know Javascript)[1], and Eloquent Javascript[2].

[1]https://github.com/getify/You-Dont-Know-JS [2]https://eloquentjavascript.net/