Giter VIP home page Giter VIP logo

library's Introduction

The Odin Project - Full Stack JavaScript

Project: Library

A single page web application to store all books on your "to-read" list. No storage has been implemented (for now), so all data will be lost upon refresh.

Live Version

Photo of Library in use

The main focus of this project was to get a better understanding of more advanced JavaScript concepts.

Constructors construct

The first new concept encountered in this project was the constructor. Constructors are used to create and initialize new objects that will have similar properties and methods.

Syntax:

// Creation of a constructor
function Constructor(arg1, arg2) {
  this.property1 = arg1
  this.property2 = arg2
  this.doSomething = function() {
    // do something
  }
}
// Instantiation of object ***don't forget the 'new' keyword***
const variable = new Constructor(arg1, arg2)

More on 'this' below.

Prototypes

Prototypes are objects that the original object inherits from. The original object has access to all of its prototype's methods and properties.

Syntax:

Constructor.prototype.doSomething = function() {
  // do something
}

This is similar to line 23 above in that both functions are defined as methods and can be called with:

variable.doSomething()

However, defining the method on the constructor itself will create a new copy of the function for each object that it instantiates. In contrast, using the prototype to define the method allows each instantiated object to access the same function without creates copies of it. This is increasingly important as the project grows in size.

Prototypes can also be copied to be used as prototypes of other constructors, but it is important to use Object.create() to create said copy, and not overwrite the original prototype.

Syntax:

Constructor2.prototype = Object.create(Constructor.prototype)

Constructor2.doSomething()
// does the thing

'this' is Where the Fun Begins

'this' refers to the context of a function invocation. The 4 types of function invocation in JavaScript are: (1) function invocation; (2) method invocation; (3) constructor invocation; and (4) indirect invocation.

An excellent resource to help understand 'this' in different contexts can be found here.

Binding Functions to Tie 'this' Together

Due to the ever-changing value of 'this', the article above discusses the .bind() method (as well as others) that can be used to create functions with a predefined value of 'this'. I found this method incredibly useful and wanted to highlight the syntax, below, as a reminder:

Syntax:

const myObject = {property1: 'value1'};

function myFunction() {
  console.log(`this: ${this}`);
}

const boundFunction = myFunction.bind(myObject);
boundFunction(); 
// 'this: [object Object]" ('[object Object]' is the reference to myObject)

library's People

Contributors

jzaager avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.