Skip to content
Open
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
70 changes: 54 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
/* The four rules for 'this';
* in your own words. explain the four rules for the "this" keyword below.
* 1.
* 2.
* 3.
* 4.
* 1. whenever this is in the global scope, this will point to the window object
* 2. if a function is called before a dot, that object before the dot is what this points to.
* 3. when this is used inside a constructor function it refers to the specific new object that is made each time.
* 4. when the call or apply methods are used this becomes explicitly defined, it points to the objects passed into those methods.
* write out a code example of each explanation above
*/

// First Rule

const Vehicle = function (traits) {
this.make = traits.make;
this.size = traits.size;
};
const camry = new Vehicle({make: 'toyota', size: 'sedan'});
camry.message = function () { return `beep beep, i am a ${this.size}`; }
console.log(camry.message());
camryBeep = camry.message;
console.log(camryBeep());
// Second Rule

Vehicle.prototype.message = function () { return `beep beep, i am a ${this.size}`; }
console.log(camry.message());
// Third Rule

const tundra = new Vehicle ({make: 'toyota', size: 'truck'});
console.log(tundra.message());
// Fourth Rule * you may want to use your third rule's example to accomplish this

// explain closure

console.log(camryBeep.call(tundra));
// explain closure
//closure is when a variable is declared inside the scope of one function but can be accessed by other functions by calling that function that contains it
function foo () {
console.log(this); // what does this point to?
console.log(this); // what does this point to? The global window
};

const counterFunction = () => {
// this code is broken. figure out why, and tell us where the closure is when you fix it
let count = 0; // this count variable was not declared. it could not be declared inside changeCount because count must be accessed at total.
const changeCount = (value) => {
count += value;
};
Expand Down Expand Up @@ -52,16 +63,41 @@ console.log(counter.total());
// your options object should have "make", "model", "year" properties on it
// assign these properties you pass in with options to the constructors 'this' object.
// add a speak() method to your object that when called will log out the car's make model and year.
// function Car(options) {
// this.make = options.make,
// this.model = options.model,
// this.year = options.year
// this.speak = function () { return`${this.make} ${this.model} ${this.year}`;}
// }


// when you're done un comment the next few lines and run the file here in node `node app.js`.

// const herby = new Car({make: 'Volkswagen', model: 'Beetle', year: '1963'});
// console.log(herby.speak());
// const goldfinger = new Car({make: 'Aston Martin', model: 'DB5', year: '1964'});
// console.log(goldfinger.speak());
//


// once you get done with this, redo it all using the class keyword and a constructor function.
// once you get done with this, redo it all using the class keyword and a constructor function.
class Car {
constructor (make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}

}
Car.prototype.speak = function() {
return `${this.make} ${this.model} ${this.year}`
}
const herby = new Car('Volkswagen', 'Beetle', '1963');
console.log(herby.make);
console.log(herby.speak());
const goldfinger = new Car('Aston Martin', 'DB5', '1964');
console.log(goldfinger.speak());


// extra credit

Expand All @@ -75,7 +111,9 @@ while(n >= 1) {
}
// write a function called countDown that does the exact same thing as above, but calls itself until it can't anymore.
// hint-> your base case will look like the logic in the while loop.




const counting = (number) => {
console.log(number);
number--;
if (number >= 1) counting(number);
};
counting(10);