I’ve Cracked the Code, Please Call Tech Support- Episode 10, The Phantom Math.Random

I’m still working, but this has been a very eventful week! New medicine, presidential election, November being a thing…it’s a lot! But we keep on going, and coding is a big part of that for me.

  1. Describe one thing you’re learning in class today.

The major focus of today has been objects in coding. An object is very handy once you can wrap your head around it. My instructor used a good metaphor for it, basically thinking about an object like a suitcase- it’s something that holds a variety of other things.

If you have an item called Suitcase, it can hold values. These values can be Toothbrush, Shirts, Shoes, Pants, Dresses, anything you want! And you can access that information as well, while still being able to conveniently have all of those values in your suitcase.

They are all individual, too! Even if you have two suitcases with the same items in them, they are still two separate, individual suitcases at the end of the day.

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

Well, first of all, we shouldn’t be using var! That is the old way of creating a variable. The new way is to use either const (a variable you don’t want to change) and let (a variable that you do expect to change).

Now, on to your actual question.

function Person(){} is what’s called a function declaration. A function that’s declared, but not executed, is saved for later use in the code. We can invoke this later, basically.

var person = Person() gives a bit more information to work with. Like we spoke about earlier, we have a variable (var person). Once it’s been stored as a variable, we can then use that variable as a function.

var person = new Person() looks pretty similar, eh? By adding the word ‘new’, we are indicating that this is a function constructor. We can set up a template object, and when we use the ‘new’ keyword, we can just fill out the info of our new object using that template.

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

This is a bit of a confusing topic, honestly, but we can get through this!

As Google Developer Expert Todd Motto discusses in his article on ultimatecourses.com, in HTML we have things called attributes, and we can update these attributes through properties that exist on elements. We can do this via JavaScript.

Let’s say that we have an input box that we want to work with. In the HTML it may look something like this:

<input type=”text” value=”0">

If we want to update the attribute, we can use the value property:

const input = document.querySelector(‘input’);

input.value = 66;

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

You can use a number of methods to iterate, or loop, over an object and array item. You can use a for loop (a bit wordy, but very clear about what you’re doing), a .forEach (essentially a for loop, but cleaner and shorter), a .map (which creates a new array using the modifications you specify), a reduce (which combines all the items in an array). There are more, but these will give you a great start in using arrays to their fullest extent.

5. What is an event loop?

This is one that I had to do some research on, but it’s a pretty interesting concept.

JavaScript can only run one piece of code at a time. It’s pretty dang fast at doing it, but still, one at a time. So, in order for it to run successfully, things need to be ordered in a particular way. This is where the event loop comes in, as well as something called the Callback Stack, and the Queue.

The Callback Stack tracks the order of when functions are being executed. The Queue is a list of messages that are processed when their time comes. This allows for certain actions to be delayed, etc.

This brings us to the Event Loop. This Loop checks out the Call Stack and Queue, and when the stack is empty it takes the first thing in the Queue and executes it. Then we start again, looping through.

6. What is the difference between call stack and task queue?

This is what we discussed above! To put it another way, the Call Stack is a stack of routines that need to be processed, and the Queue is a queue of persistent messages that are processed when their time comes. This is all done via the Event Loop.

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

There are a few differences here, so let’s work our way through them.

The ES6 class defines a new object, appends functions to its prototype, and ensures that the operator ‘keyword’ used inside the class only refers to the object that is being created.

ES5 Function constructors, on the other hand, look and work more or less the same except for inheritance. These can only be executed with a new operator, rather than the ES6 ‘keyword’. These also allow any function to be used as a constructor, and reusable object.