Skip to content
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
84 changes: 83 additions & 1 deletion assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,31 @@
- When eating an edible, it should be pushed into a "stomach" property which is an array.
- Give persons the ability to poop.
- When pooping, the stomach should empty.
*/
function Person(name, age) {
this.name = name;
this.age = Number(age);
this.greet = `${this.name} ${this.age}`;
this.eatEdible = 'I eat I eat I eat...Muah HaHa';
this.stomach = [];
}
Person.prototype.eating = function (food) {
return this.stomach.push(food);
}

Person.prototype.pooping = function () {
return this.stomach= [];
}

var femi = new Person('Oluwafemi', '29');
femi.eating('eba');
console.log(femi.stomach)
femi.eating('Rice');
console.log(femi.stomach)
femi.pooping();
console.log(femi.stomach)

/*
TASK 2

- Build a Car constructor that takes model name and make.
Expand All @@ -55,22 +79,80 @@
- A crashed car can't be driven any more. Attempts return a string "I crashed at x miles!", x being the miles in the odometer.
- Give cars the ability to be repaired.
- A repaired car can be driven again.

*/
function Car (modelName, carMake) {
this.model = modelName;
this.make = carMake;
this.drive = true;
this.crash = true;
this.repair = true;
this.odometer = 0;
}
Car.prototype.driveDistance = function () {
return this.odometer += 50;
}
Car.prototype.carCrashed = function () {
if (this.crash === true) {
console.log(`I crashed at ${this.driveDistance()} miles!`);
return this.drive = false
}
return this.drive = true;
}
Car.prototype.carRepaired = function () {
if (this.repair === true) {
console.log(`You have fixed your ${this.model}. Take it for a spin!!!`)
return this.drive;
}
return this.drive = false;
}
var vehicle = new Car('Toyota', 'Sedan');
/*
TASK 3

- Build a Baby constructor that subclasses the Person built earlier.
- Babies of course inherit the ability to greet, which can be strange.
- Babies should have the ability to play, which persons don't.
- By playing, a string is returned with some text of your choosing.
*/
function Baby (name, age,) {
Person.call(this, name, age);
this.play = `a-ga, a-ba, a-da`;
}

Baby.prototype = Object.create(Person.prototype);
Baby.prototype.playBaby = function () {
return `Baby likes to play.. Baby likes to play`;
}
var baby = new Baby('Jamie', 1, 'trains');

/*
TASK 4

Use your imagination and come up with constructors that allow to build objects
With amazing and original capabilities. Build 3 small ones, or a very
complicated one with lots of state. Surprise us!

*/
function Life(name, placeOfBirth, death) {
this.name = name;
this.place = placeOfBirth;
this.death = Number(death);
this.college = true;
this.goodLife = `${this.name}, I guess you lived a good Life.`;
this.justOK = `${this.name}, life did nt turn out how you planned it. At least you still enjoyed your old age. I guess you can't complain too much`
this.notJustOk = `${this.name}, Hmmmmmmmmmmmmmmmm`
}
Life.prototype.checkLife = function () {
if (this.death < 50) {
return this.notJustOk;
} else if (this.death < 70 ) {
return this.justOK;
} else {
return this.goodLife;
}
}

var james = new Life('James', 'Nigeria', '80')
/*

STRETCH TASK
Expand Down
29 changes: 22 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,32 @@
*/

// Principle 1

// WIndow Binding happens in the global scope.
// code example for Window Binding
var myName = 'Oluwafemi';

// Principle 2

// Whenever a function is called by a preceding dot
// code example for Implicit Binding
function myName () {
return this;
}

// Principle 3
var a = {
name: 'nnn',
}

// Principle 3
// Whenever a Constructor function is used.
// code example for New Binding

// Principle 4

// code example for Explicit Binding
function MyName (name) {
this.name = name;
}
// Principle 4
// Whenever JavaScript’s call or apply method is used
// code example for Explicit Binding
function myRealName (name, address) {
return `${name}, your address is ${address}`
}
var me = myRealName.call(null,'Oluwafemi', 'Gbagada, Lagos' );
var me = myRealName.bind(null,'Oluwafemi', 'Gbagada, Lagos' );