JavaScript: The Hard Parts, v2

Higher Order Functions Q&A

Will Sentance

Will Sentance

Codesmith
JavaScript: The Hard Parts, v2

Check out a free preview of the full JavaScript: The Hard Parts, v2 course

The "Higher Order Functions Q&A" Lesson is part of the full, JavaScript: The Hard Parts, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Will answers a question from the audience about execution context within a for loop, and whether the function arguments are pass by value or pass by reference.

Preview
Close

Transcript from the "Higher Order Functions Q&A" Lesson

[00:00:00]
>> Speaker 1: You say when we're passing a function or passing a function definition, is that also just like a function reference? Because you can't change it and it's already in memory?
>> Will Sentance: Yeah, so I was deciding whether or not to include my little bonus piece. Let's just keep it as simple as this.

[00:00:15]
This array here in global is saved in global. It is then parsed in as a link back to where it was saved in global. Now I was saving global without a label. It was just there in global, but we actually parse in a link to it. We are not copying it in.

[00:00:30]
We're parsing, yes, a reference to where it was initially saved in global memory. If you remember our earlier version where we passed in my array, we saved that 1, 2, 3 array in global, and then we just passed a link back to it. And that's why we don't want to change inside of copy array and manipulate the data we pass in because that would be changing the global data, which is hard to predict what's then gonna happen without function.

[00:00:56]
They call it a side effect. But instead, here, we create a brand new array, and take each individual element, stick it in that new array. And our input array stays unmutated, stays unchanged. Nice, by Dan, yeah, go ahead, Dan.
>> Dan: But the function, can you call it a function reference, or is it a function definite-

[00:01:13]
>> Will Sentance: I see, yeah, we're again, same thing here. With our function, again, it's a link back to the original saved multiplyBy2 function. So any function, array, or object, when it's saved, if we ever use it anywhere else and link to it, we're actually referring back to its original saved version.

[00:01:35]
Very nice from Dan, excellent. Peter?
>> Peter: Kind of a behind the scenes thing, when the for loop is running, does it kind of get its own execution context?
>> Will Sentance: No, it gets its own protected namespace. That is to say that if we use let to define the i, that i is only available inside that block scope, which is the little curly braces.

[00:01:56]
But it is not an execution it is not putting that on the call stack. It's just another protected space in memory for those labeled names. Okay, yeah, yeah, nice question.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now