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
60 changes: 49 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,60 @@
/* The four rules for 'this';
* in your own words. explain the four rules for the "this" keyword below.
* 1.
* 2.
* 3.
* 4.
* 1. When 'this' doesn't refer to anything in local scope, it will look outside its scope for 'this'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What scope will it point to if there isn't anything defined globally?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that make it undefined?

* 2. When using dot notation, 'this' refers to what comes to the left of the dot
* 3. In a constructor function, 'this' refers to the specific object returned by the constructor
* 4. When it comes to call() and apply(), 'this' is defined explicitly in the method

* write out a code example of each explanation above
*/

// First Rule
function myFunction() {
console.log(this);
};

// Second Rule
const myCat = {
name: 'Buddy',
age: 2,
sayMeow: function() {
console.log('Meow, my name is `${this.name}`');
}
};

myCat.sayMeow();

// Third Rule
class Kitty {
constructor(options) {
this.name = options.name;
this.age = options.age;
this.sayMeow = function() {
console.log('Meow, my name is `${this.name}`');
};
};
};

const griffin = new Kitty({name: 'Griffin', age: 1});
const buddy = new Kitty({name: 'Buddy', age: 2});
// Fourth Rule * you may want to use your third rule's example to accomplish this
buddy.sayMeow.call(griffin);

// explain closure
// A function inside of a function that accesses a variable outside of its scope

function foo () {
console.log(this); // what does this point to?
console.log(this); // what does this point to?
};
// Something in the global scope/window

const counterFunction = () => {
// this code is broken. figure out why, and tell us where the closure is when you fix it
let count = 0; // initialize count
const changeCount = (value) => {
count += value;
count += value; //accesses count here
};
return {
return {
increment: () => {
changeCount(1);
},
Expand All @@ -52,13 +80,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.
class Car {
constructor(options) {
this.make = options.make;
this.model = options.model;
this.year = options.year;
this.speak = function() {
console.log( `${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.
Expand Down