From 5db64994d75d818f24e37016574bb6778c05375e Mon Sep 17 00:00:00 2001 From: kammagik Date: Wed, 21 Mar 2018 15:45:24 -0700 Subject: [PATCH 1/2] mini --- constructors.js | 1 - recursion.js | 12 +++++++++++- this.js | 48 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/constructors.js b/constructors.js index d0c9de2..d5d3c67 100644 --- a/constructors.js +++ b/constructors.js @@ -30,4 +30,3 @@ function Cat(options) { // }); // // foofie.grow(); - diff --git a/recursion.js b/recursion.js index b4b66d1..668b08b 100644 --- a/recursion.js +++ b/recursion.js @@ -10,9 +10,15 @@ while (n <= 10) { // write a recursive - function called countToTen that mimics the while loop above. // code here + const countToTen = (n) => { + if (n <= 10) return; + console.log('While Loop', n); + countToTen(n+1); +}; +countToTen() // when you code is ready, un-comment the next line and run the file -// console.log(countToTen()); + console.log(countToTen()); /* ================ Next Problem ================= */ // Problem 2: @@ -28,6 +34,10 @@ const factorial = n => { console.log(factorial(5)); // write the above function in a recursive way. + const factorial = (n) => { + if (n===1) return 1; + return n * factorial + } // when your code is ready, un-comment the next line and run the file // console.log(recursiveFactorial()); diff --git a/this.js b/this.js index 895b5fe..d0019cc 100644 --- a/this.js +++ b/this.js @@ -1,10 +1,10 @@ -/* The for principles of "this"; +* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Window Binding-information from whatever console you are using +* 2. Implicit Binding-the object before dot, thats the this(what its referring to) +* 3. New Biding-function that creates an object then returns it +* 4. Explicit Binding-overide what objects get set to with .call and .apply * * write out a code example of each explanation above */ @@ -14,15 +14,53 @@ console.log('hello world!'); // Principle 1 // code example for Window Binding +function winBind(name) { + return name; +} +winBit('Kelly') // Principle 2 // code example for Implicit Binding +const myTown = { + weather: 'rain', + mood: 'relaxed' + myAtmosphere: function(weather) { + console.log(this.mood) + } +} +myTown.myAtmosphere(); // Principle 3 // code example for New Binding +const myTown = { + weather: 'rain', + mood: 'relaxed' + myAtmosphere: function(weather) { + console.log(this.mood + this.weather) + console.log(this); + }; +} +const newWeather = new myTown('sunny'); + +newWeather.mood(); + + // Principle 4 // code example for Explicit Binding +const myTown = { + weather: 'rain', + mood: 'relaxed' + myAtmosphere: function(weather) { + console.log(this.mood + this.weather) + console.log(this); + }; +} +const newWeather = new myTown('sunny'); +const newMood = new myTown('exhausted'); + +newWeather.myAtmosphere.call(); +newMood.myAtmosphere.apply(); From 2c4eaae331151bedc1b5c612fece2116b933acf4 Mon Sep 17 00:00:00 2001 From: kammagik Date: Thu, 22 Mar 2018 17:23:15 -0700 Subject: [PATCH 2/2] added classes, constructors, recursion --- classes.js | 13 ++++++++++++- constructors.js | 8 ++++++++ recursion.js | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/classes.js b/classes.js index c275caa..072964a 100644 --- a/classes.js +++ b/classes.js @@ -2,10 +2,22 @@ // problem #1 // convert the Animal constructor function from 'constructors.js' into an ES6 class +class classAnimal { + Animal(options) { + this.name = options.name; + this.grow=options.grow; + } +} // problem #2 // convert the Cat constructor function from 'constructors.js' into an ES6 class +class classCat extends classAnimal { + Cat(options) { + super(options) + this.isclassCat = options.isclassCat; + } +} // if everything is setup properly the code below will print 'Foofie grew larger!' @@ -16,4 +28,3 @@ // }); // // foofie.grow(); - diff --git a/constructors.js b/constructors.js index d5d3c67..987741e 100644 --- a/constructors.js +++ b/constructors.js @@ -6,9 +6,15 @@ function Animal(options) { this.name = options.name; + this.grow=options.grow; } + Animal.prototype.grow = function { + return `${this.name} grew larger!`; + } + // add 'grow' to Animal's prototype here +grow.prototype.Object.create(Animal.prototype); // problem #2 // setup Cat to inherit from Animal @@ -18,6 +24,8 @@ function Animal(options) { function Cat(options) { // invoke Animal here with .call + Animal.call(options); + this.grow = options.grow; } // connect the prototypes here diff --git a/recursion.js b/recursion.js index 668b08b..98981cf 100644 --- a/recursion.js +++ b/recursion.js @@ -17,6 +17,7 @@ while (n <= 10) { }; countToTen() + // when you code is ready, un-comment the next line and run the file console.log(countToTen()); /* ================ Next Problem ================= */