Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-Exercise-Protoype #255

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 65 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,33 @@ 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.stoamch = [];
}

Person.prototype.toString = function(){
return `${this.name}, ${this.age}`;
}

const bennett = new Person('bennett', 24);


console.log(bennett.toString());
bennett.eat('pizza');
bennett.eat('bagel');
console.log(bennett.stomach);
bennett.poop();
console.log(bennett.stomach);


/*
Expand All @@ -63,10 +82,34 @@ 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){
return this.tank = this.tank + gallons;
}


Car.prototype.drive = function(distance){
this.odometer = this.odometer + distance;
this.tank = this.tank/this.milesPerGallon;
if(this.tank === 0){
return `I ran out of fuel at ${this.odometer}!`;
}
}

const nissan = new Car('Altima', 26);

const ford = new Car ('F150', 32);

console.log(nissan.fill(52));

console.log(nissan.drive(1));

/*
TASK 3
Expand All @@ -75,18 +118,29 @@ function Car() {
- Besides the methods on Person.prototype, babies 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}`;
}

const Natalia = new Baby ('Natalia', 6, 'stuffed animals');

console.log(Natalia);
console.log(Natalia.play());

/*
TASK 4
In your own words explain the four principles for the "this" keyword below:
1.
2.
3.
4.
1. If scope is global, its object referenced will be the window/console
2. Whenever a dot precedes a function it will always reference the object to its left
3. when usinng constructors, this refers to the object that is being created as a result of the constructor function
4. when using call/apply methods you explicity define wht 'this' refers to
*/


Expand Down
Loading