Week 3 of JavaScript

Pamela White
3 min readJul 16, 2021

--

Describe one thing you’re learning in class today.

Several functions that each do one or a few things are better than one long function that does everything. This is both preparation for real world situations (you or others may need just one part of a solution to reuse) and makes it easier to debug (you can test each piece).

What’s the difference between: function Person(){}, var person = new Person(), var person = Person()

function Person(){}

This is a constructor function. It sets up the structure of what will be objects called Person. It’s like a blueprint to create objects.

var person = new Person()

This is how to call the function to create a new Person object. Putting new in front of a function call, JavaScript creates an object inside the function, itself, and calls it this. Then, it returns that object, this, to the statement where it was invoked. This house is built.

var person = Person()

This is a variable, person, that stores the constructor function, Person()

What’s the difference between an “attribute” and “property”?

Attributes are assigned as part of HTML. They can only be strings

Properties are assigned as part of the DOM. They can be other data types (Boolean, string, etc.).

What language constructions do you use for iterating over object properties and array items?

For loops and while loops iterate over arrays. The higher order function, forEach() iterates over all the items in an array. Other higher order functions, such as some(), every(), map(), and filter() also iterate over arrays.

For in iterates over objects. Additionally, object.keys() takes an object as a parameter and iterates over the properties returning them (the keys) as an array. You can then loop over that array using previously mentioned methods.

What is the event loop?

The event loop is a bit like the executor of events. It acts continually on the task cue. As soon as there is a task in the task cue, it acts on it. Then it waits until there’s something added to the cue.

What’s the difference between a call stack vs task queue?

The task queue is the list of jobs that need to be done. All tasks get added to the task queue. Anything happening asynchronously gets added to the task queue, but doesn’t get acted on immediately (i.e., it doesn’t enter the call stack until it’s turn comes up).

A call stack is all the instructions or the routine being executed right now. This has to be completed before can go; follows the execution logic until the call stack is empty, then goes back to the event queue/

The three together — event loop, call stack, and stack cue — are like a server at a restaurant. The server’s attention is the event loop. They figure out when something needs to be done — they see a couple come in and have to greet them, they see an empty water glass and need to fill it, they see a table ready to order and need to take an order. They put all these things on their task queue. In fact, our language for how the brain works mimics a computer’s functioning. We call the organization that the server does executive function. Attending to tasks that need to be done — the ones on the task queue — is an executive functioning skill. The task queue, itself, is like our working memory. The server is holding it all in their brain so they can work on it in due time. The call stack is the routine they are currently doing. The server will finish the task he’s doing, even while other tasks on the tasks queue get added (e.g., he finishes pouring the water even though a new couple came to be seated) or asynchronous tasks are being done and added (e.g., the chef has finished an order and rang the bell indicating it’s ready to be served).

What are the differences between ES6 classes and ES5 function constructors?

Function constructors are a way to instantiate objects. Clases allow you to have that object, like it was constructed using a function constructor, and extend it to add new information.

--

--