Skip to content

Commit 64a8fe4

Browse files
author
Jesse Hood
committed
completed constructors.js
1 parent bfe7312 commit 64a8fe4

3 files changed

Lines changed: 74 additions & 20 deletions

File tree

constructors.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ function Animal(options) {
88
this.name = options.name;
99
}
1010

11+
1112
// add 'grow' to Animal's prototype here
13+
Animal.prototype.grow = function() {
14+
console.log(`${this.name} grew larger!`);
15+
}
1216

1317
// problem #2
1418
// setup Cat to inherit from Animal
@@ -18,16 +22,18 @@ function Animal(options) {
1822

1923
function Cat(options) {
2024
// invoke Animal here with .call
25+
Animal.call(this, options);
2126
}
2227

2328
// connect the prototypes here
29+
Cat.prototype = Object.create(Animal.prototype);
2430

2531
// if everything is setup properly the code below will print 'Foofie grew larger!'
2632
// uncomment the code below to test your solution
2733

28-
// const foofie = new Cat({
29-
// name: 'foofie',
30-
// });
31-
//
32-
// foofie.grow();
34+
const foofie = new Cat({
35+
name: 'foofie',
36+
});
37+
38+
foofie.grow();
3339

recursion.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ while (n <= 10) {
1010
// write a recursive - function called countToTen that mimics the while loop above.
1111

1212
// code here
13-
13+
let countToTen = (n = 0) => {
14+
console.log(`While Loop ${n}`);
15+
if (n < 10) return countToTen(n+1);
16+
}
1417
// when you code is ready, un-comment the next line and run the file
15-
// console.log(countToTen());
18+
console.log(countToTen());
1619
/* ================ Next Problem ================= */
1720

1821
// Problem 2:
1922

2023
const factorial = n => {
21-
let result = 1;
22-
for (let i = 2; i <= n; i++) {
23-
result *= i;
24-
}
25-
return result;
24+
if (n === 0) return 1;
25+
return n * factorial(n - 1);
2626
};
2727

2828
console.log(factorial(5));

this.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,66 @@
99
* write out a code example of each explanation above
1010
*/
1111

12-
// Principle 1
12+
// Principle 1: Window Binding - window/global this points to that object
1313

14-
// code example for Window Binding
14+
function sayName(name) {
15+
console.log(`hello my name is ${name}`);
16+
console.log(this);
17+
}
1518

16-
// Principle 2
19+
sayName('Ryan');
1720

18-
// code example for Implicit Binding
21+
// Principle 2: Implicit Binding - object before the dot becomes this
1922

20-
// Principle 3
23+
const myObj = {
24+
greeting: 'hello',
25+
sayHello: function(name) {
26+
console.log(`${this.greeting} ${name}`);
27+
console.log(this);
28+
}
29+
}
2130

22-
// code example for New Binding
31+
myObj.sayHello('Ryan');
2332

24-
// Principle 4
33+
const sayNameFunc = (obj) => {
34+
obj.sayName = function () {
35+
console.log(`Hello my name is ${this.name}`);
36+
console.log(this);
37+
}
38+
};
2539

26-
// code example for Explicit Binding
40+
const me = {name: 'Ryan'};
41+
const you = {name: 'Freddy' };
42+
43+
sayNameFunc(me);
44+
console.log(me);
45+
sayNameFunc(you);
46+
console.log(you);
47+
48+
me.sayName();
49+
you.sayName();
50+
51+
// Principle 3: New Binding - the object where the new keyword called.
52+
53+
function CordialPerson(greeter) {
54+
this.greeting = 'hello';
55+
this.greeter = 'greeter';
56+
this.speak = function() {
57+
console.log(this.greeting + this.greeter);
58+
console.log(this);
59+
}
60+
}
61+
62+
const jerry = new CordialPerson('Newman');
63+
const newman = new CordialPerson('Jerry');
64+
65+
jerry.speak();
66+
newman.speak();
67+
68+
console.log(jerry);
69+
console.log(newman);
70+
71+
// Principle 4: Explicit Binding - Whenever javascript's call or apply method is used, this is explicitly
72+
73+
jerry.speak.call(newman);
74+
newman.speak.apply(jerry);

0 commit comments

Comments
 (0)