Quiz
- Still not too sure about the meaning of first class items.
The phrase first class means it can be stored in variables, passed as values, etc. In JavaScript, it's common to pass a function as an argument to another function/method.
- difference between global and local variables
Global variables are shared among all functions. Useful, but require coordination among functions (possibly authored by different people) and can conflict (two people want to use
x
for different purposes).Local variables solely belong in a particular scope (typically a function), and aren't shared at all.
- So var is used to declare a local variable, then how do we declare a global variable?
Strangely, you *omit* the
var
when you refer to it in a function: - Anonymous functions / Can you please review anonymous functions?
Anonymous functions work the same as named functions:
Where it gets interesting is a function as an argument:
- Do named functions and anonymous functions differ by the amount of storage that they take up?
Not in any significant way. Yes, the browser needs to store the name of the function, but that's a trivial amount.
- Why do you need anonymous functions? Why couldn't you do the
arithmetic without a function?
It's not the arithmetic that is the point; that's just to keep the functions simple.
The point is that we often need to create a function solely to give it to the browser so that it can call the function when some event occurs. That's called event-handling, and it's how we do web programming.
It's often convenient just to create the function anonymously and pass it immediately to the browser. Even if you never do this, it's standard practice in web programming, so you need to wrap your head around it.
- Is there a reason that websites use JavaScript and not Python?
Because when the first interactive browser was being created (Netscape), he wanted to put Scheme in the browser (not Python) but his bosses insisted that it have Java-like syntax. See Netscape and JavaScript. He completed the first version in ten days!!!.
- Can you go over creating functions and using them directly in the HTML? Thanks!
We've seen a few examples already. We can open the console and try a few more.
- could we go over the forEach method, thank you! can you review the forEach method? How does it work (whats its output)?
It runs a function (which is an argument) on each of the values in the array.
The method returns no value, and the function doesn't need to return a value.
- Could you explain the forEach Method more? Why is the third argument (the array itself) ignored?
It's not ignored, but it's rarely used. Here's a case where it is:
- Can anonymous functions be used multiple times? Or does the information within the function "reset" after it has been declared the first time?
Yes. We just saw a bunch of examples where the function passed to forEach is called five times (once for each word in the quote).
Functions *always* start over ("reset"), whether they are anonymous or named. That's true in Python and all other languages. (That's not perfectly true, but close enough.)
- I don't think I understand the point of the arrow function. Why use it at all?
It's really succinct:
- I'm still confused about the difference between let and var. Can you do an example in class?
The difference is small and subtle. If the difference matters to your code, you're probably doing something wrong. Here's a brief example:
If you want to hear more, let's talk in office hours, but you truly should not worry about this.
- JavaScript as an object-oriented language vs. other object-oriented languages.
We'll talk about OOP in a few weeks.