From 9833d6e65fd3db11d36b786ff4d0007c5075d6dd Mon Sep 17 00:00:00 2001 From: dev Date: Mon, 27 May 2019 14:48:47 +0800 Subject: [PATCH] Answered Question 1 --- practice.js | 39 +++++++++++++++++++++++++++++++++------ user.json | 4 ++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/practice.js b/practice.js index c5812b5..d025185 100644 --- a/practice.js +++ b/practice.js @@ -2,14 +2,19 @@ // 1) What is the purpose of the 'this keyword'? //Answer + //The purpose of the 'this keyword' is to refer the object where it belongs to. // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? //Answer + //The four rules that govern the 'this keyword' are, first if the new keyword is used when calling the function, this inside the function is a brand new object. Second, if apply, call, or bind are used to call a function, this inside the function is the object that is passed in as the argument. Third, if a function is called as a method — that is, if dot notation is used to invoke the function — this is the object that the function is a property of. In other words, when a dot is to the left of a function invocation, this is the object to the left of the dot. Last, if a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it’s window. + + // 3) What does .bind do? //Answer + //Bind creates a new function. //Next Problem @@ -20,6 +25,13 @@ //getUsername --> which is a function that returns the current object's username property. *Don't use 'user' instead use the 'this' keyword* //Code Here + var user = { + username: 'diana', + email : 'diana@boom.camp', + getUsername (){ + return this.username; + } + } //Now, invoke the getUsername method and verify you got the username of the object and not anything else. @@ -54,7 +66,22 @@ var getYear = function(){ //Note(no tests) //Code Here +function Car(make, model, year, move){ + this.make = make; + this.model = model; + this.year = year; + this.move = 0; + return { + moveCar () { + this.move +=10; + return this.move; + } + } +} + +getPriusYear = getYear.bind(prius); +getMustangYear = getYear.bind(mustang); //New Problem @@ -62,23 +89,23 @@ var getYear = function(){ var myUser = { username: 'iliketurtles', age: 13, - email: 'iliketurtles@gmail.com' -}; - -var getMyUsername = function() { + email: 'iliketurtles@gmail.com', + getMyUsername : function() { return this.username; +} }; -var userName = getMyUsername(); //Fix this +var userName = myUser.getMyUsername(); //Fix this //Above you're given an object, and a function. What will the getMyUsername function return? //Note(no tests) //Answer Here + //undefined //In the example above, what is the 'this keyword' bound to when getMyUsername runs? //Answer Here - + //none //Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'. diff --git a/user.json b/user.json index 4ac80a0..802040f 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Diana Lyn C. Geromo", + "email": "diana.geromo@boom.camp" }