From c5624d64b4e981303a23dcc67f902a190e30360b Mon Sep 17 00:00:00 2001 From: Nikki Date: Wed, 9 Aug 2017 21:33:34 -0400 Subject: [PATCH 1/4] Defined this and added some examples -- Not Completed --- app.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index eba22ae..20eafc0 100644 --- a/app.js +++ b/app.js @@ -1,21 +1,44 @@ /* The four rules for 'this'; * in your own words. explain the four rules for the "this" keyword below. -* 1. -* 2. -* 3. -* 4. -* write out a code example of each explanation above +* 1. Anytime you have a function that happens to be contained in the global scope, the value of "this" when used inside of that function will be set to the window object. +* 2. If a function is called by a preceding dot, any object that comes before that dot will be "this" +* 3. When a constructor function is used, "this" in this case, refers to the specific instance of that object that is not only created by also returned by that constructor function. +* 4. In any circumstance when JavaScript's call or apply method are implemented, "this" is then explicitly defined. */ // First Rule +const welcomeGreeting = (name) => { + console.log(`Welcome to the party, ${name}`); + console.log(this); +}; + +welcomeGreeting('Person'); // Second Rule +const anotherGreeting = { + newGreet: 'Hi, Nice to meet you, ', + greetPerson: function(name) { + console.log(`${this.newGreet} ${name}`); + } +}; + +anotherGreeting.greetPerson('Person'); // Third Rule +function newCharacter (options) { + this.typeClass = options.typeClass; + this.weaponSelection = options.weaponSelection; + this.playerMessage = function() { + console.log(`Your player class is ${this.typeClass}` + ` and your weapon of choice is ${this.weaponSelection}` ); + } +}; + +const playerOne = new newCharacter({typeClass: 'Mage', weaponSelection: 'Staff'}); +console.log(playerOne); // Fourth Rule * you may want to use your third rule's example to accomplish this -// explain closure +// explain closure function foo () { console.log(this); // what does this point to? @@ -61,7 +84,7 @@ console.log(counter.total()); // 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. // extra credit @@ -75,7 +98,3 @@ 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. - - - - From cdfc024e95f651d715a83939dabde143868fa577 Mon Sep 17 00:00:00 2001 From: Choong K Date: Thu, 10 Aug 2017 08:20:30 -0700 Subject: [PATCH 2/4] Add ex for 3rd rule of this, implement countDown with recursion, fix counter --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 20eafc0..d1d94ac 100644 --- a/app.js +++ b/app.js @@ -34,7 +34,7 @@ function newCharacter (options) { }; const playerOne = new newCharacter({typeClass: 'Mage', weaponSelection: 'Staff'}); -console.log(playerOne); +console.log(playerOne); // Fourth Rule * you may want to use your third rule's example to accomplish this @@ -45,7 +45,8 @@ function foo () { }; const counterFunction = () => { - // this code is broken. figure out why, and tell us where the closure is when you fix it + // this code is broken. figure out why, and tell us where the closure is when you fix it + let count = 0; const changeCount = (value) => { count += value; }; From 1f78997f3042eea7d8ec14f2222f45a761a458a8 Mon Sep 17 00:00:00 2001 From: Choong K Date: Thu, 10 Aug 2017 08:47:36 -0700 Subject: [PATCH 3/4] Add Car constructor, will need to refactor to ES6 --- app.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index d1d94ac..0cf52d0 100644 --- a/app.js +++ b/app.js @@ -34,18 +34,25 @@ function newCharacter (options) { }; const playerOne = new newCharacter({typeClass: 'Mage', weaponSelection: 'Staff'}); -console.log(playerOne); +console.log(playerOne); // Fourth Rule * you may want to use your third rule's example to accomplish this +anotherGreeting.greetPerson.call(anotherGreeting, 'Person'); + // explain closure +// Closure is a function that is returned by another function and maintains bindings between the variable assignments local to the outer function the closure +// was create in. + function foo () { console.log(this); // what does this point to? }; +// this will point to the global object + const counterFunction = () => { - // this code is broken. figure out why, and tell us where the closure is when you fix it + // this code is broken. figure out why, and tell us where the closure is when you fix it let count = 0; const changeCount = (value) => { count += value; @@ -78,11 +85,17 @@ console.log(counter.total()); // add a speak() method to your object that when called will log out the car's make model and year. // when you're done un comment the next few lines and run the file here in node `node app.js`. +const Car = function(options){ + this.make = options.make; + this.model = options.model; + this.year = options.year; + this.speak = () => console.log(`${this.model} ${this.year}`); +} -// 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. @@ -99,3 +112,11 @@ 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 countDown = (n) => { + if(n === 0) return; + console.log(n); + return countDown(n-1); +}; + +countDown(10); \ No newline at end of file From 59cb866ab58734ea40d5e0b2816ae1c651d89d00 Mon Sep 17 00:00:00 2001 From: Choong K Date: Thu, 10 Aug 2017 08:53:03 -0700 Subject: [PATCH 4/4] Refactor Car class to ES6 --- app.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 0cf52d0..5f2bc97 100644 --- a/app.js +++ b/app.js @@ -85,12 +85,22 @@ console.log(counter.total()); // add a speak() method to your object that when called will log out the car's make model and year. // when you're done un comment the next few lines and run the file here in node `node app.js`. -const Car = function(options){ - this.make = options.make; - this.model = options.model; - this.year = options.year; - this.speak = () => console.log(`${this.model} ${this.year}`); -} +// const Car = function(options){ +// this.make = options.make; +// this.model = options.model; +// this.year = options.year; +// this.speak = () => console.log(`${this.model} ${this.year}`); +// } +class Car{ + constructor(options) { + this.make = options.make; + this.model = options.model; + this.year = options.year; + this.speak = () => console.log(`${this.model} ${this.year}`); + }; +}; + + const herby = new Car({make: 'Volkswagen', model: 'Beetle', year: '1963'}); console.log(herby.speak());