Classes, Inheritance, Functional Programming


Classes, Inheritance, Functional Programming

1. Name 3 advantages to Test Driven Development

  1. TDD tells you whether your last change (or refactoring) broke previously working code.
  2. TDD forces the radical simplification of the code. You will only write code in response to the requirements of the tests.
  3. The programmer’s productivity is increased.

source: DZone

2. In what case would you need to use beforeEach() or afterEach() in a test suite?

These are helper functions meant to be used when there is work that needs to be done repeatedly for multiple tests, such as initializing or clearing a database that a test will interact with. beforeEach() and afterEach() can also handle asynchronous code by taking a ‘done’ parameter or returning a promise.

Source: jest docs

3. What is one downside of Test Driven Development

When you’re unsure about functions to be developed, especially at early stage of development, writting a lot of test for a function could be time consuming. Especially if you have to get rid of that function in later days, then those tests would be useless.

4. What’s the primary difference between ES6 Classes and Constructor/Prototype Classes?

Classes: Although JavaScript is object-oriented language, it isn’t a class-based language. It’s a prototype-based language. But their constructors are weird. So at ES6, they introduced classes. They look like classes of a class-based language, but deep inside, it’s still a constructor style. But, they’ve made the inherit and the whole thing look much simpler.

Constructors: They are used to create objects

5. Name a use case for a static method

Static methods are used to implement functions that belong to the class, but not to any particular object of it.

6. Write an example of a Higher Order function and describe the use case it solves

Higher order functions are functions that operate on other functions, either by taking them as arguments or by returning them. In simple words, A Higher-Order function is a function that receives a function as an argument or returns the function as output.

example:

let’s say, we want make arr2 based on arr1, but every element in arr1 will be doubled.

Without Higher-order function:

const arr1 = [1, 2, 3];
const arr2 = [];
for(let i = 0; i < arr1.length; i++) {
  arr2.push(arr1[i] * 2);
}
// prints [ 2, 4, 6 ]
console.log(arr2);

With Higher-order function:

const arr1 = [1, 2, 3];
const arr2 = arr1.map(item => item * 2);
console.log(arr2);

Vocabulary

Term Definition
functional programming a way of programming that breaks down complex problems into smaller pieces and avoids state-changing and mutable data
pure function function alwways gives same return value for same arguments, cannot depend on any mutable state.
higher-order function They are functions that operate on other functions, either by taking them as arguments or by returning them
immutable state the state of an object after creation where it cannot be changed or modified
object collection of properties that consist of key value pairs, when property value is a function it’s called a method
object-oriented programming (OOP) model that organizes software design around objects and focuses on that data rather than functions and logic
class blueprint for creating an object
prototype special property of a function, a way for an object to inherit properties and methods
super used to access properties and methods on an object’s parent
inheritance a child objects get properties from a parent object
constructor function that creates an instance of an object
instance one iteration of an object created with key ‘new’ from constructor function
context refers to the object to which a function belongs
this points to the inheriting object when an inherited function is executed
Test Driven Development (TDD) the discipline of writing a test for something before writing any actual solution code
Jest package via npm that allows you to run tests
Continuous Integration (CI) development practice that involves sharing code into a shared repo and testing from there before changes are committed
unit test test that checks again one individual unit of code which is usually something like just one function