diff --git a/bike-shop/src/stage3-methods.js b/bike-shop/src/stage3-methods.js index 2f2fc5a..e84e96a 100644 --- a/bike-shop/src/stage3-methods.js +++ b/bike-shop/src/stage3-methods.js @@ -28,7 +28,6 @@ Tire.prototype.isFlat = function () { Tire.prototype.puncture = function () { this._isFlat = true - //this.tires[0]._isFlat = true } diff --git a/bike-shop/src/stage4-inheritance.js b/bike-shop/src/stage4-inheritance.js index 2281fd5..e139f94 100644 --- a/bike-shop/src/stage4-inheritance.js +++ b/bike-shop/src/stage4-inheritance.js @@ -3,14 +3,69 @@ class Frame { } class Tire { - // your code here + } class Bike { - // your code here + constructor() { + this.tires = [new Tire, new Tire] + this.frame = new Frame + this.brakes = { + back : true, + front : true + } + this.rings = [3, 7] + } +} + +class MountainBike extends Bike { + constructor() { + super(); + this.tires[0].type = 'dirt'; + this.tires[1].type = 'dirt'; + this.frame.style = 'mountain'; + this.shocks = 20; + } + adjustShocks(newSagSetting){ + this.shocks = newSagSetting; + } +} + +class BMXBike extends Bike { + constructor() { + super(); + this.brakes.front = false; + this.tires[0].diameter = 20; + this.tires[1].diameter = 20; + } +} + +class RacingBike extends Bike { + constructor(val) { + super(val); + this.tires[0].type = 'road'; + this.tires[1].type = 'road'; + this.frame.style = 'racing'; + //work on this below + this.rings[0] = 3; + this.rings[1] = 10; + } + gearSpeeds(){ + return this.rings[0] * this.rings[1]; + } } + + +//let mountainBike = new MountainBike() +//let racingBike = new RacingBike() + + + module.exports = { Bike: Bike, + MountainBike: MountainBike, + BMXBike: BMXBike, + RacingBike: RacingBike // you'll need to export new classes here } diff --git a/index.html b/index.html index 2223032..82f542f 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,6 @@ - + diff --git a/jsinfo/errorCreatingAnInstance.js b/jsinfo/errorCreatingAnInstance.js new file mode 100644 index 0000000..9a2eb28 --- /dev/null +++ b/jsinfo/errorCreatingAnInstance.js @@ -0,0 +1,26 @@ +/* +Here’s the code with Rabbit extending Animal. + +Unfortunately, Rabbit objects can’t be created. +What’s wrong? Fix it. +*/ +class Animal { + + constructor(name) { + this.name = name; + } + +} + +class Rabbit extends Animal { + constructor(name) { + //need to add a supper function to allow inheritance + super(name) + //now with the super() + //this.name = name; + this.created = Date.now(); + } +} + +let rabbit = new Rabbit("White Rabbit"); // Error: this is not defined +alert(rabbit.name); diff --git a/jsinfo/extendClock.js b/jsinfo/extendClock.js new file mode 100644 index 0000000..238148c --- /dev/null +++ b/jsinfo/extendClock.js @@ -0,0 +1,57 @@ +class Clock { + // ({ ... }) allows the item to be passed in as a object literal + constructor({template}){ + this._template = template; + } +// prototype not allowed in classes. bound to the class function + _render() { + let date = new Date(); + + let hours = date.getHours() + if (hours < 10) hours = '0' + hours; + + let mins = date.getMinutes(); + if (mins < 10) min = '0' + mins; + + let secs = date.getSeconds(); + if (secs < 10) secs = '0' + secs; + + let output = this._template + .replace('h', hours) + .replace('m', mins) + .replace('s', secs); + + console.log(output); + } + +//clock.stop() will stop the clock + stop() { + clearInterval(this._timer); + console.log("clocked stopped"); + }; + +//clock.start() would then restart the clock + start() { + this._render(); + //setInterval(function, milliseconds, param1, param2, ...) + this._timer = setInterval(() => this._render(), this._precision); + } +} + +// pass inheritence from Clock to ExtendedClock +class ExtendedClock extends Clock { + + constructor(val) { + //allows for the inheritence to be manipulated + super(val) + //set the variable + let {precision=1000} = val; + this._precision = precision; + } +} + +let clock = new ExtendedClock({ + template: 'h:m:s', + precision: 1000 +}); + clock.start(); diff --git a/jsinfo/rewritePrototype.js b/jsinfo/rewritePrototype.js index 44af2da..e83108c 100644 --- a/jsinfo/rewritePrototype.js +++ b/jsinfo/rewritePrototype.js @@ -69,8 +69,8 @@ Clock.prototype.stop = function() { }; Clock.prototype.start = function() { - this.render(); - this.timer = setInterval(() =>this.render(), 1000); + this._render(); + this.timer = setInterval(() =>this._render(), 1000); }; diff --git a/jsinfo/rewriteToClass.js b/jsinfo/rewriteToClass.js index 21edd44..68f1be7 100644 --- a/jsinfo/rewriteToClass.js +++ b/jsinfo/rewriteToClass.js @@ -41,6 +41,7 @@ let clock = new Clock({template: 'h:m:s'}); class Clock { // ({ ... }) allows the item to be passed in as a object literal constructor({template}){ + //wanted to console.log(typeof({template})); this._template = template; }