From 583add11cca6ebcc714b0196d3afbd09b0f7d583 Mon Sep 17 00:00:00 2001 From: JT Coates Date: Wed, 23 Mar 2022 21:14:09 -0600 Subject: [PATCH] prototypes and inheritance homework --- index.js | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 419761f6..6ecfc753 100644 --- a/index.js +++ b/index.js @@ -42,13 +42,24 @@ Airplane.prototype.land = function () { + It should return a string with `name` and `age`. Example: "Mary, 50" */ -function Person() { - +function Person(name, age) { + this.name = name; + this.age = age; + this.stomach = []; +} +Person.prototype.eat = function(edible) { + if(this.stomach.length < 10){ + this.stomach.push(edible); + } +} +Person.prototype.poop = function () { + this.stomach = []; +} +Person.prototype.toString = function(){ + return `${this.name}, ${this.age}`; } - - - +const mary = new Person('Mary', 50) @@ -68,11 +79,18 @@ function Person() { + The `drive` method should return a string "I ran out of fuel at x miles!" x being `odometer`. */ -function Car() { - +function Car(model, milesPerGallon) { + this.model = model; + this.milesPerGallon = milesPerGallon; + this.tank = 0; + this.odometer = 0; +} +Car.prototype.fill = function(gallons){ + this.tank = this.tank + gallons; +} +Car.prototype.drive = function(distance) { + this.tank = this.tank - this.milesPerGallon; } - - /* TASK 3 - Write a Baby constructor subclassing Person. @@ -80,18 +98,21 @@ function Car() { - Besides the methods on Person.prototype, babies also have the ability to `.play()`: + Should return a string "Playing with x", x being the favorite toy. */ -function Baby() { - +function Baby(name, age, favoriteToy) { + Person.call(this, name, age); + this.favoriteToy = favoriteToy +} +Baby.prototype = Object.create(Person.prototype) +Baby.prototype.play = function() { + return `Playing with ${this.favoriteToy}` } - - /* TASK 4 In your own words explain the four principles for the "this" keyword below: - 1. - 2. - 3. - 4. + 1. Window Binding + 2. Implicit Binding + 3. Explicity Binding + 4. New Binding */