diff --git a/short-response.md b/short-response.md index 1b57dbc..bb292ad 100644 --- a/short-response.md +++ b/short-response.md @@ -11,6 +11,8 @@ console.log(b); ``` +It logs 1 because b is assigned the value in the global scope when the function is called, +so when the is logged, it prints 1. 2. What does the following code log? Why? ```javascript @@ -22,6 +24,8 @@ console.log(context); ``` +It returns the Window object, because this is in the global scope. + 3. What will the code below output? Explain the difference, if any, between this output and that of problem 8. ```javascript const obj = { @@ -34,6 +38,9 @@ console.log(context); ``` +A property is assigned to a function inside the object, and storing that function to the variable context. Instead of the window object being returned, +we get the value of func, which in this case is a function. +This is because it is not pointing to window anymore - it's inside the context of obj. 4. We expect the following code to log `34000` but instead we are getting `35000`. What is the bug and how can we fix it? ```javascript @@ -56,11 +63,21 @@ console.log(computer.total()); ``` +Change specialDiscount() from a function declaration to an arrow function, +`this` will the be bound the object fixing the issue. 5. What is a closure? +A closure is a function inside a function. +It hides variables since the outer function creates a scope for the inner function. 6. What are the benefits of using a constructor functions to create object instances instead of a factory function? + The ability create methods in prototype, which will apply to the constructor object as well as all the objects that are constructed with it is a benefit of using constructor functions. + You can also create new objects and have variables point to the object you create through them. 7. What is the `__proto__` property and how does it differ from the `protoype` property? -8. What is inheritance? \ No newline at end of file + Prototype is a property of a function, while `proto` is an object that points to the prototype and also lists things like the constructor and methods that can be used for all objects. + +8. What is inheritance? + Inheritance is when an object that is created by a constructor takes in parameters or values from the constructor object and uses it in itself. + Inheriting methods, for example, takes the methods of the constructor prototype, which allows the object to use the methods as well. diff --git a/solutions.js b/solutions.js index e69de29..c74260f 100644 --- a/solutions.js +++ b/solutions.js @@ -0,0 +1,74 @@ +//Question 1 +const makeCounter = function(start) { + return function() { + start++; + return start; + } +}; + +//Question 2 +const makeFriendList = function() { + const arr = []; + return { + addFriend(name) { + arr.push(name); + return `${name} successfully added.` + }, + removeFriend(name) { + if(arr.includes(name)) { + arr.splice(arr.indexOf(name), 1); + return `${name} successfully removed.` + } + return `${name} not found.` + }, + displayFriends() { + return arr; + } + } +} + +//Question 3 +function Teacher(name, school, grade, subject) { + this.name = name; + this.school = school; + this.grade = grade; + this.subject = subject; + this.students = []; +} + +Teacher.prototype.addStudent = function(studentName) { + this.students.push(studentName); + return this.students.length; +} + +Teacher.prototype.changeSchools = function(schoolName) { + this.school = schoolName; + return this.school; +} + +//Question 4 +function Quadrilateral(side1, side2, side3, side4) { + this.side1 = side1; + this.side2 = side2; + this.side3 = side3; + this.side4 = side4; +} + +Quadrilateral.prototype.getPerimeter = function() { + return (this.side1 + this.side2 + this.side3 + this.side4); +} + +//Question 5 +function Rectangle(length, width) { + Quadrilateral.call(this, length, width); + this.length = length; + this.width = width; + this.side3 = length; + this.side4 = width; +} + +Rectangle.prototype = Object.create(Quadrilateral.prototype); + +Rectangle.prototype.getArea = function() { + return this.length * this.width; +} \ No newline at end of file