Week 5 Learning Objectives

NPM

  1. Explain the purpose of the package.json file and node_modules directory.

  2. Given multiple choices, identify the difference between npm’s package.json and package-lock.json files.

  3. Use npm --version to check what version is currently installed and use npm to update itself to the latest version.

  4. Use npm init to create a new package and npm install to add a package as a dependency. Then use require to import the module and utilize it in a JavaScript file.

  5. Given an existing GitHub repository, clone the repo and use npm to install it’s dependencies.

JavaScript Classes

  1. Define a constructor function using ES5 syntax.
function Book(title, series, author) {
  this.title = title;
  this.series = series;
  this.author = author;
}
  1. Define a method on the prototype of a constructor function.
Book.prototype.getInformation = function () {
  if (this.series) {
    return `${this.title} (${this.series})`;
  } else {
    return this.title;
  }
}
  1. Declare a class using ES6 syntax.
class Book {
  constructor(title, series, author) {
    this.title = title;
    this.series = series;
    this.author = author;
  }
}
  1. Define an instance method on a class (ES6).
class Book {
  // constructor

  getInformation() {
    if (this.series) {
      return `${this.title} (${this.series})`;
    } else {
      return this.title;
    }
  }
}
  1. Define a static method on a class (ES6).
class Book {
  // constructor

  static getUniqueAuthors(...books) {
    let authors = [];
    books.forEach(book => {
      if (!authors.includes(book.author)) {
        authors.push(book.author)
      }
    })
    return authors;
  }
}
  1. Instantiate an instance of a class using the new keyword.
const theGrapesOfWrath = new Book('The Grapes of Wrath', null, 'John Steinbeck');
  1. Implement inheritance using the ES6 extends syntax for an ES6 class.
class Book extends CatalogItem {
  // Book code
}
  1. Utilize the super keyword in a child class to inherit from a parent class.
class CatalogItem {
  constructor(title, series) {
    this.title = title;
    this.series = series;
  }

  getInformation() {
    if (this.series) {
      return `${this.title} (${this.series})`;
    } else {
      return this.title;
    }
  }
}

class Book extends CatalogItem {
  constructor(title, series, author) {
    super(title, series);
    this.author = author;
  }
}

class Movie extends CatalogItem {
  constructor(title, series, director) {
    super(title, series);
    this.director = director;
  }

  // extending the functionality of the parent's getInformation()
  getInformation() {
    let result = super.getInformation();
    if (this.director) {
      result += ` [directed by ${this.director}]`;
    }
    return result;
  }
}
  1. Utilize module.exports and require to import and export functions and class from one file to another.

// catalog-item.js

class CatalogItem {
  // CatalogItem code
}

module.exports = CatalogItem
// book.js

const CatalogItem = require('./catalog-item');

class Book extends CatalogItem {
  // Book code
}

module.exports = Book
- In this next example, we are exporting a multiple items from a file. We are assigning an object to module.exports that contains each item we are exporting. When we import from this file, we can either import the whole object, or destructure right in our import
// classes.js

class CatalogItem {
  // CatalogItem code
}

class Book extends CatalogItem {
  // Book code
}

class Movie extends CatalogItem {
  // Movie code
}

module.exports = { Book, Movie}
// index.js

const classes = require('./classes');
const Movie = classes.Movie;
const Book = classes.Book;

// Or, destructuring in the import statment:
// const { Movie, Book } = require('./classes');

const theGrapesOfWrath = new Book('The Grapes of Wrath', null, 'John Steinbeck');
const aNewHope = new Movie('Episode 4: A New Hope', 'Star Wars', 'George Lucas');

Object-Oriented Programming

  1. The three pillars of object-oriented programming

  2. The SOLID principles

  3. How to apply the Law of Demeter

Assessment Format