Eloquent Javascript: Chapter 4 (Data Structures: Objects and Arrays)

preview_player
Показать описание

-Arrays
-Array Methods
-String Methods and Properties
-Objects
-Rest Parameters
Рекомендации по теме
Комментарии
Автор

Around 1:18:00, when you come across the part that says you can "spread an array into the function call", there is actually a way to do this and get the result you are expecting.

The book does a poor job in mentioning "spreading the array" without explaining "spread syntax" which looks exactly like a rest parameter. When you define a function with a rest parameter, it takes an unlimited amount of arguments and all of the arguments will be put into an array and passed into the function. So when we define the function "function addOne(...sampleArray)", and then use it with "addOne(1, 2, 3, 4)" our arguments are turned into an array "[1, 2, 3, 4]" and passed through to the rest of the function as the variable "sampleArray".

When you have defined an array in your example, "array = [1, 2, 3, 4]" and then pass it through to this function, "addOne(array)" it is actually passing through "addOne([1, 2, 3, 4])". What happens is that, our function, sees one argument (our array of [1, 2, 3, 4]) instead of multiple arguments, and then it turns that into a new array called "sampleArray" where the first and only argument (the array [1, 2, 3, 4]) becomes index[0] of our new array "sampleArray". So our array is nested within the array called "sampleArray". Lol, a bit confusing but sample array becomes [ [12, 3, 4] ]. To further illustrate this, if you called the function "addOne(array, 5, 6)", sampleArray would become
"[ [1, 2, 3, 4], 5, 6]"

To actually pass through the individual values of our array as individual arguments, we can use something called "Spread Syntax" which uses the same ellipses as the rest parameter. So if you have an array, lets use your example of "array = [1, 2, 3, 4]", spread syntax is used by placing an ellipses before the variable name. "...array". It would look like this when being used as an argument when calling our function:


addOne(...array)

This is the same as addOne(1, 2, 3, 4).


Whereas:

addOne(array)

is equivalent to addOne([1, 2, 3, 4])


Hope that isn't too confusing and helps anyone peeking in the comments section!

noiseissound
Автор

I keep coming back to these videos, this video is very helpful and your presentations are simple, on point and superb, Inshaallah I'll try to summarise things the way you do in these presentations. After beating my head around Eloquent javascript, this lecture and previous lecture feels like fresh air. Thank you so much Chris Sir.

nuhakhangr
Автор

Thanks again for another great and easy to follow video. You break everything down and make it easy to understand. Hope you had a great Thanksgiving! :)

Dave-sooe
Автор

i am prepping for my interview, coud you mind adding those ppy slides into the description

nishanthpatil
Автор

Where can we find the powerpoints for this ?

TheMarketUnion
Автор

I really enjoy the moments when we forget to equate a value to a variable and we get lost😅😅🙌

yarcxxhassan
Автор

Heyy, thanks for the video, i wanted to ask if it's possible to get the slides you used for the video

Iamtunmise
Автор

where i can find the text from the slides?

alb-negru
join shbcf.ru