Giter Club home page Giter Club logo

js-objects-prototypes's Introduction

General Assembly Logo

JavaScript Prototypes

Prerequisites

Objectives

  • Define methods on custom objects by attaching them to the prototype

Preparation

  1. Fork and clone this repository.
  2. Change to the new directory.
  3. Install dependencies.
  4. Create and checkout a new branch, training

Prototypes

In the previous lesson, we saw how to use constructors to de-duplicate effort in creating new objects that share attributes. We learned that we should never define a method inside a constructor function. So how should we get behavior in our custom objects?

Remember wonderWoman?

let wonderWoman = {
  name: 'Diana Prince',
  alias: 'Wonder Woman',

  usePower: function() {
    return 'Deflects bullets with bracelets';
  }
}

We made a nice Hero constructor to take care of the attributes .

const Hero = function(name, alias, power) {
  this.name = name;
  this.alias = alias;
  this._power = power;
};

Code along: add methods to prototypes

  1. Create usePower and attach it to the constructor function's prototype.
  2. Create a method to say the hero's name and alias. Attach it to the prototype.
  3. Create batman and wonderWoman. Call the method just attached.
  4. Observe that this method isn't part of objects created using the constructor function.

Lab: Add Methods to Prototypes

Change the run tracker code you made in the previous lesson to use prototypes.

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

js-objects-prototypes's People

Watchers

James Cloos 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.