From 4fc1543930e2ed433af63c2bdceb5af9683a4134 Mon Sep 17 00:00:00 2001 From: Sara Fritz <31226863+sarafritz88@users.noreply.github.com> Date: Thu, 7 Sep 2017 19:24:27 -0500 Subject: [PATCH] hw no 4 or ec --- app.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index eba22ae..ed38785 100644 --- a/app.js +++ b/app.js @@ -1,28 +1,47 @@ /* The four rules for 'this'; * in your own words. explain the four rules for the "this" keyword below. -* 1. -* 2. -* 3. -* 4. +* 1. When a function is in the global scope, this inside the function will be the window object. +* 2. When a function is called with a dot, this is whatever is preceding the dot +* 3. When used with a constructor, this refers to the specific variable +* 4. Call and Apply use the first argument passed to them to determine what this points to * write out a code example of each explanation above -*/ +*/ // First Rule +function sayHello(name) { + console.log('Hi' + name); + console.log(this); +} +sayHello('Sara') // Second Rule +const Dog = { + name; 'Cooper'; + func: function () { + return this.name; + } +}; +console.log(Dog.func()); // Third Rule +function User(users) { + this.name = name +} +const richard = new User; +console.log(richard.name); // Fourth Rule * you may want to use your third rule's example to accomplish this -// explain closure +// explain closure Functions that have access to outer functions variables and scope function foo () { console.log(this); // what does this point to? }; +This points 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; // there was no place to start the count const changeCount = (value) => { count += value; }; @@ -52,17 +71,33 @@ 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 = () => { + 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()); + 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. - +class Cars { + function (options) { + this.model = options.model; + this.make = options.make; + this.year = options.year; + } + speak(){ + return `${this.make} ${this.model} ${this.year}`; + } +} // extra credit // we didn't touch on Recursion in the lecture yet, but you're going to build a recursive function now