Git Product home page Git Product logo

js-oo-introduction-001's Introduction

JavaScript OO Introduction

Objective

Learn how object oriented programing works in JavaScript.

Overview

After sitting in front of your computer for the last 48 hours writing some awesome code, you realize you should get some exercise too. Maybe a bike ride? But before you put on some spandex shorts and hop on a bike, let's write some more code!

You will be building two JavaScript objects: an owner and a bike. The owner has several bikes, can build a new bike, and can go on a ride using any of their bikes. Each bike has 2 wheels, a color, and knows what kind of condition it's in. After a long bike ride, the owner will need to give the bike a tune up.

Here's a quick reference for creating objects with JavaScript:

// constructor (unique for each object)
function Pet (name, kind) {
  this.name = name;
  this.kind = kind;
}

// instance methods
Pet.prototype.speak = function(){
    if ( this.kind == 'dog' ){
      return 'woof';
    } else if (this.kind == 'cat'){
      return 'meow';
    } else{
      return 'hello, I\'m a ' + this.kind;
    }
  }
};

// creating a new object
vivian = new Pet('Lady Vivian the Terrible', 'dog');
zargon = new Pet('Zargon', 'fish');
vivian.speak();
// -> woof
zargon.speak();
// -> hello, I'm a fish

Instructions

  • Flesh out the Bike and Owner constructors
  • Bike instances have a name, type, color, and 'condition' properties
    • condition is by default equal to 'ready to go!'
  • Owner instances have a name and bikes properties
  • Bikes have a constant called WHEELS set to 2
  • Bikes have #takeForARide method that, if more than 50 miles are traveled it sets the condition property to 'needs a tune up'
  • Bikes have a #tuneUp method that sets the condition property to 'ready to go!'
  • Owners have a #buildBikes method that creates a new bike that they then own
  • Owners have a #rideBike method that only works on bikes that they own
    • this method takes a number of miles as an argument

Tests

Run the Jasmine test and build out your objects. The test should help guide you.

# Install jasmine:
gem sources -a http://flatiron:[email protected]
gem install learn-co

# Run Jasmine test suite:
learn  # runs only in the terminal
learn -b # runs also in the browser

Resources

javascriptissexy.com โ€” "JavaScript Objects in Detail"

js-oo-introduction-001's People

Contributors

tsiege avatar mrmitchboyer avatar fs-lms-test-bot avatar fislabstest avatar sarogers avatar

Watchers

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