diff --git a/app.js b/app.js index eba22ae..9d5e4f9 100644 --- a/app.js +++ b/app.js @@ -1,30 +1,54 @@ /* The four rules for 'this'; * in your own words. explain the four rules for the "this" keyword below. -* 1. -* 2. -* 3. -* 4. +* 1. Whenever the 'this' keyword is used in a global function it refers to the 'Window' that the function is in. +* 2. Whenever Dot Notation is used the Object to the left is the reference to 'this' +* 3. Whenever a 'constructor' is used the 'this' keyword refers to the instantiated object +* 4. You can specifically assign the 'this' keyword using Call() or Apply() * write out a code example of each explanation above */ // First Rule +() => console.log(this); // Second Rule +const person = { + greet: 'Hi my name is ', + writeName: function(test) { + console.log(this.greet + test); + } +} +person.writeName('Brandon'); // Third Rule +function Greeting() { + this.greet = 'Hello '; + this.sayHi = function(name) { + console.log(this.greet + name); + } +}; + +const newPerson = new Greeting(); +newPerson.sayHi('Brandon'); + // Fourth Rule * you may want to use your third rule's example to accomplish this +//newPerson.sayHi.apply('Bob'); // explain closure +// variables Can Only Be Access From The Outside Of The Scope function foo () { - console.log(this); // what does this point to? + console.log(this); // what does this point to? }; +//Window const counterFunction = () => { // this code is broken. figure out why, and tell us where the closure is when you fix it + + const count = 0; // <-- Added The Count Variable To The Function Scope + const changeCount = (value) => { - count += value; + count += value; // <---- The Bottom Return Does Not Have access To This Varialbe }; return { increment: () => { @@ -34,7 +58,7 @@ const counterFunction = () => { changeCount(-1); }, total: () => { - return count; + return count; } } }; @@ -52,13 +76,23 @@ 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 () { + console.log(this.make, this.model); + }; + } + // 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()); + 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. diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +}