Prompt 203

Describe one thing you’re learning in class today.

Today in class we are continuing the lessons on JSON Objects and Arrays. We went over how to access data in complex data structures.

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

function Person() {}

Function Declaration. Declares a function statement (statements perform an action) but does not execute, however, it does get registered into the global namespace.

var person = Person()

Function Expression
A variable ‘var person’ has been defined and contains a value reference to a Person function. Any JavaScript Expressions (including Function Expressions) always returns a value. This may also be an Anonymous function if no name has been assign to a function but wrapped in parenthesis to be interpreted as an expression.

var person = new Person()

Function Constructor
By adding the keyword ‘new’. We are instantiating a new object of the Person class constructor. A function declaration is just a regular function unless it has been instantiated, it then becomes a class constructor.

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

An attribute is the initial state when rendered in the DOM. A property is the current state.

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

For objects:

  • for loops - for (var property in obj) { console.log(property); }. However, this will also iterate through its inherited properties, and you will add an obj.hasOwnProperty(property) check before using it.
  • Object.keys() - Object.keys(obj).forEach(function (property) { ... }). Object.keys() is a static method that will lists all enumerable properties of the object that you pass it.
  • Object.getOwnPropertyNames() - Object.getOwnPropertyNames(obj).forEach(function (property) { ... }). Object.getOwnPropertyNames() is a static method that will lists all enumerable and non-enumerable properties of the object that you pass it.

For arrays:

  • for loops - for (var i = 0; i < arr.length; i++). The common pitfall here is that var is in the function scope and not the block scope and most of the time you would want block scoped iterator variable. ES2015 introduces let which has block scope and it is recommended to use that instead. So this becomes: for (let i = 0; i < arr.length; i++).
  • forEach - arr.forEach(function (el, index) { ... }). This construct can be more convenient at times because you do not have to use the index if all you need is the array elements. There are also the every and some methods which will allow you to terminate the iteration early.

What is the event loop?

JavaScript has a concurrency model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks

What is the difference between call stack and task queue?

In short, a job queue is a queue of things to do and a call stack is a stack of routines. A job would have variables assigned to it, and a call stack would be the abstract implementation. So a job could “call” a method from a call stack

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

ES6 Class

ES6 class basically does the work of defining a new object and appending functions to its prototype. They can be considered as the syntax base for constructor functions. ES6 class allows the developers to instantiate objects using the new operator. They also ensure the developer that this keyword which is basically used inside the class only refers to the object that is being created by the developer.

ES5 Function Constructors

ES5 Function constructors work and look the same but the main difference is observed when the developer uses the Inheritance property. These can only be executed with the help of a new operator. ES5 function constructors focus on how the objects are instantiated. ES5 function constructor focus on implementing the reusable object creation code. Any function can be used as a constructor.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store