diff --git a/classes.js b/classes.js index c275caa..4bfb1b1 100644 --- a/classes.js +++ b/classes.js @@ -2,18 +2,30 @@ // problem #1 // convert the Animal constructor function from 'constructors.js' into an ES6 class - +class Animal { + constructor(options) { + this.name = options.name; + } + grow() { + return `${this.name} grew larger!` + } +} // problem #2 // convert the Cat constructor function from 'constructors.js' into an ES6 class +class Cat extends Animal { + constructor(options) { + super(options); + } +} // if everything is setup properly the code below will print 'Foofie grew larger!' // uncomment the code below to test your solution -// const foofie = new Cat({ -// name: 'foofie', -// }); -// -// foofie.grow(); +const foofie = new Cat({ + name: 'foofie', +}); + +console.log(foofie.grow()); diff --git a/constructors.js b/constructors.js index d0c9de2..cf70e1f 100644 --- a/constructors.js +++ b/constructors.js @@ -9,6 +9,13 @@ function Animal(options) { } // add 'grow' to Animal's prototype here +Animal.prototype.grow = function() { + return `${this.name} grew larger!` +} + +// Person.prototype.speak = function () { +// return `Hello, my name is ${this.name}`; +// }; // problem #2 // setup Cat to inherit from Animal @@ -18,16 +25,18 @@ function Animal(options) { function Cat(options) { // invoke Animal here with .call + Animal.call(this, options); } // connect the prototypes here +Cat.prototype = Object.create(Animal.prototype); // if everything is setup properly the code below will print 'Foofie grew larger!' // uncomment the code below to test your solution -// const foofie = new Cat({ -// name: 'foofie', -// }); -// -// foofie.grow(); +const foofie = new Cat({ + name: 'foofie', +}); + +console.log(foofie.grow()); diff --git a/recursion.js b/recursion.js index b4b66d1..f9130ba 100644 --- a/recursion.js +++ b/recursion.js @@ -10,9 +10,16 @@ while (n <= 10) { // write a recursive - function called countToTen that mimics the while loop above. // code here +let m = 1; +let countToTen = function(m) { + if (m === 10) return console.log('While Loop II', m); + console.log('While Loop II', m); + m++; + return countToTen(m); +} // when you code is ready, un-comment the next line and run the file -// console.log(countToTen()); +console.log(countToTen(m)); /* ================ Next Problem ================= */ // Problem 2: @@ -20,7 +27,7 @@ while (n <= 10) { const factorial = n => { let result = 1; for (let i = 2; i <= n; i++) { - result *= i; + result *= i; // 1 * 2, 2 * 3, 6 * 4, 24 * 5 } return result; }; @@ -28,6 +35,18 @@ const factorial = n => { console.log(factorial(5)); // write the above function in a recursive way. +let fresult = 1; +let recursiveFactorial = (k) => { + if (k === 1) return console.log(fresult); + fresult *= k; + // console.log("K is: " + k + "fresult: " +fresult); + k--; + recursiveFactorial(k); + +} +// factorialT(5); + + // when your code is ready, un-comment the next line and run the file -// console.log(recursiveFactorial()); +console.log(recursiveFactorial(5)); diff --git a/this.js b/this.js index 895b5fe..2cc4c48 100644 --- a/this.js +++ b/this.js @@ -8,21 +8,46 @@ * * write out a code example of each explanation above */ +1. Implicit Binding. When you call a function within an object (called a method), the method's this +statement uses properties attached to the object. In the following, hello is the object and myFunc() +is a method. Example: hello.myFunc() . + +2. Explicit Binding. Is when you want to pass data that's not attched to an object or function to +do a task on that object or function. If the object or function is not connected to some variable +that has data that you want to use on the object or function, you use EXPLICIT BINDING. Such as +.call(), .apply() , or .bind(). In the following hello is a function and arVar is a variable passed +to the hello function. Example: hello.call(argVar) . + +3. New Binding. It's used to create a new object from an object constructor, ES5 and ES6 constructor +function (class in ES6) look different, but the syntax to create new object is the same. When you create +a new object with the "New" keyword (let objname = New ClassConstruct(prop1, prop2, prop3, etc.), the +object's constructors this.prop are bound to the new object created. So, the .this references the new object. + Example: let newvar = New Hello("prop1","prop2", "etc of new object") . + + +4. Window Binding. When you don't have data for a task, such as a function doing a task with data, and the function didn't receive the +data by other methods (Implicit binding, Explicit binding, or New binding), than the function will look for +data in the container, or window. The following doesn't have anything specfic to console.log, and it will +log the windows's (or container's) this references. Example: console.log(this) -console.log('hello world!'); // Principle 1 // code example for Window Binding +console.log(this) // Principle 2 // code example for Implicit Binding +hello.myFunc() // Principle 3 // code example for New Binding +let newvar = New Hello("prop1","prop2", "etc of new object") // Principle 4 // code example for Explicit Binding + +hello.call(argVar) \ No newline at end of file diff --git a/yarn-error.log b/yarn-error.log new file mode 100644 index 0000000..d23e477 --- /dev/null +++ b/yarn-error.log @@ -0,0 +1,32 @@ +Arguments: + /home/linuxbrew/.linuxbrew/Cellar/node/9.6.1/bin/node /usr/share/yarn/bin/yarn.js run + +PATH: + /home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/abueno/.local/bin:/home/abueno/bin + +Yarn version: + 1.5.1 + +Node version: + 9.6.1 + +Platform: + linux x64 + +npm manifest: + No manifest + +yarn manifest: + No manifest + +Lockfile: + No lockfile + +Trace: + Error: Couldn't find a package.json file in "/home/abueno/LambdaSchool/JavaScript-II-Mini" + at new MessageError (/usr/share/yarn/lib/cli.js:186:110) + at /usr/share/yarn/lib/cli.js:40048:15 + at Generator.next () + at step (/usr/share/yarn/lib/cli.js:98:30) + at /usr/share/yarn/lib/cli.js:109:13 + at