Just me or does eloquent javascript... sort of suck? Should I learn from someplace else?

In terms of explaining things, I feel like the author fails to adequately provide enough explanation (or one at all actually) so people working through the book for the first time can actually understand what's going on.

Several examples:

var numbers = [1, 2, 3, 4, 5], sum = 0;  
forEach(numbers, function(number) {
sum += number;
});
console.log(sum);
// → 15

I have no idea how this works because I don't know how "number" is recognized as one variable in the array. As in, how is the parameter "number" inside the function that's passed itself as an parameter inside forEach used? An explanation for this behaviour would be nice but it's not found within the book.

Then you've got this:

function noisy(f) {
return function(arg) {
console.log("calling with", arg);
var val = f(arg);
console.log("called with", arg, "- got", val);
return val;
};
}
noisy(Boolean)(0);

I've never seen anything like noisy(Boolean)(0), in terms of the two closed parentheses next to each other. I have no idea what this does and the book doesnt tell me. I believe earlier in the book it also did a type conversion to string for the first time without explaining what it did, and I feel this might be a type conversion too but again I'm not sure.

Explanations would be great but I'm worried that this is a trend that will keep happening as I progress.

Posted Apr 30, 2016, 12:10 AM UTC · edited

1420
birdukis•

No one source is going to be everything you need, you have questions which is normal, mess around with the code and see what happens when you change things, just reading the book will teach you little.

In general though I don't think that elonquent javascript is a great resource for someone new to programming to use. It is better once you understand programming more.

7
byvioletOP•

I definitely understand that but I just think it's weird when the book introduces code that it has never talked about before and expects you to understand it, seeing as the book is sort of an intro book. What other resource would you recommend?

3
BluesTime•

eloquent is too many times wrongly recommended for beginners... I'm still new but went through codecademy, codeschool then read Beginning Javascript and now doing a (paid) angular tutorial by watchandcode.com which is pretty great.

The thing I would have done in the start is asked myself what I wanted to build. If you have a vision for a thing it is much easier to learn and pick up the necessary bits.

8
TheHumanEquation•

I would highly recommend Tony Alicea's Javascript course on Udemy if you want to gain a deep understanding of the language. Tony puts a lot of care into crafting his courses, and has a knack for explaining concepts with clarity. It's $20 on Udemy, but you can preview the first 3.5 hours of the course for free on youtube to see if it's what you're looking for :)

3
Schorr_Thing•

I'd agree that eloquent JS isn't the best learning resource since it gets overly complex pretty quick, but looking at that second example I actually think it's a pretty good learning example, it even has that final console.log with what the final returned value is, with what that argument used was.

But I'd agree that the closed parens next to each other tripped me up for the longest time as well. Just think of it executing a returned function immediately, without saving it to a new variable. Play around with it for a bit... You'll get the hang of it :)

3
lewisje•

That probably was too clever by half: noisy(Boolean)(0) means "invoke the noisy function with the Boolean function as its argument, then invoke the function it returns, with the number 0 as its argument"; noisy takes a function and returns a function that logs its first argument, invokes that function with that first argument, and then logs the first argument and the return value of that function call.

I agree, it is confusing to see a function call on a function call.


Unlike some other languages, there is no special syntax for type conversion: It happens either automatically as needed, or deliberately by using the conversion functions or the toString or valueOf methods on an object; the exception to that is that ES6 symbols cannot be converted to strings automatically, only deliberately.


The first argument to forEach is an array, in this case, the array numbers; the second argument is a function that will be called for each element of the array, with the element as its first argument, and this function just calls its first formal parameter number, and again that looks too clever by half.

2
byvioletOP•

so noisy(Boolean)(0) is an example of a type of syntax? Would you to happen to know what this syntax is called if that's the case?

EDIT: I'm actually confused now why it's noisy(Boolean)(0) and not noisy(Boolean(0)).

2
bdenzer•

It is an example of closure. This is the most simple example I can come up with.

function setFirstNumber(first) {
  return function setSecondNumber(second) {
    return first + second;
  }
}

So now you can do

var stepOne = setFirstNumber(5);

//now stepOne is a function (that got returned from setFirstNumber) and it is waiting for an argument

stepOne(4);  //Returns 9

//but for the same result you could just use

setFirstNumber(5)(4); //Returns 9

I completely agree that this is not really for beginners. There are quite a few places where I thought the exact same thing, and I had some experience before I ever found the book.

3
lewisje•

noisy(Boolean)(0) is a function call applied to the result of another function call; this particular one makes a "noisy" function from the Boolean conversion function, then calls that "noisy" function with 0, returning false (same as Boolean(0)) and making some console logs in the meantime.

It is equivalent to

var noisyBool = noisy(Boolean);
noisyBool(0);

noisy(Boolean(0)) is a function call that passes in another function call as its argument; it is equivalent to

var bool = Boolean(0);
noisy(bool);

and this one would return a function that throws an error when invoked, because Boolean(0) is false and that's not a function:

var noisyBool = noisy(Boolean(0));
noisyBool(); // Uncaught TypeError: f is not a function
2
00mba•

That function inside of forEach brackets is a callback function. The word 'number' is just a reference to tell the callback function to accept some kind of variable, exactly like any other function. It's naming is arbitrary, which EloquentJS likely doesn't explain. forEach automatically passes a value into that callback for processing in the callback function 'for each' item in the array. Without declaring a value in the callback, forEach would not work. That's kinda how forEach works... It does something with each member of the array or object you pass in... The callback is what does something with it.

Hope this makes sense I just threw it together while pooping.

2
petakow•

You may find the "You dont know" by Kyle Simpson series more helpful. He goes through how Javascript works in its essential elements in separate short books (which are ordered). They're a great way to learn basic issues that lead to bugs and inefficient code.

2
yooossshhii•

The explanation for the forEach function is explained directly above that code.

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

It takes two arguments, an array, and an action, which is a callback. It iterates over the array, grabbing each element at array[i] and applying the action callback to it.

So, numbers is the array input, and everything after the comma to the closing parenthesis is the action callback. Maybe it would be more clear if you saw:

var sum = 0;
var sumNumbers = function(number) {
  sum += number;
}
forEach(numbers, sumNumbers);

So, on the first iteration, i = 0 and array[i] is 1, so it calls sumNumbers(1); and that adds it to sum. Then, array[i] is 2 and sumNumbers(2) is called.

As others have said, EJS isn't necessarily the best resource for super beginners as it ramps up fast and doesn't do any hand holding. You really need to make sure you understand all the previous concepts or you'll get lost quickly. Many people have to reread many of the chapters.

2
Jswizzy84•

It's more of an intermediate resource. I loved it but I already knew javascript and had even written a card game when I read it. Your problem is that functional programming is a hard concept to grasp. It's not really a big feature in a lot of other popular languages. Not many people are learning LISP outside of school and javascript shares more in common with that language then say Java. What's happing here is foreach is accepts an anonymous function as a first class object. It's basically treated the same as a variable. Other people have explained the rest. Number is just the current element in the array and the second variable is the first class anonymous function to be applied to the element.

1
radong01•

If you're looking for books, read the "You don't know javascript" series: https://github.com/getify/You-Dont-Know-JS.

1
tmetler•

I can't speak for the book but it seems like the concept you need to learn more about is first class functions.

1
bootcamporbust•

yes. it does.

use this http://watchandcode.com/courses

practical javascript

1
Kong28•

Could you expand on what you linked?

1
gordonmzhu•

Here's the full link the original commenter was referring to. It's a JavaScript course from watchandcode.com.

https://watchandcode.com/courses/practical-javascript

1
freedimension•

Actually I'm reading the book at the moment too. I totally hate the whole cats-of-aunty example. This is so far fetched that it's the exact opposite of a real world example. Don't know if he just wanted to be a hoopy frood, hopping on the train with all the cat memes, but it really sucks imho. As for whether it's for starters or intermediates: neither ... the beginner is lost with missing informations, while those already firm with programming from other languages get bored really fast by the extensive and over elaborated narration.

0
theCynx•

I started with this book. initially was okay, but later down few chapters found it annoying thanks to those squirrel examples. I have since followed other resources like javascriptissexy.com, few video tutorials on udemy and the mozilla documentation.

0