diff --git a/practice.js b/practice.js index c5812b5..b53c926 100644 --- a/practice.js +++ b/practice.js @@ -1,15 +1,15 @@ //We're in a job interview. Answer the following questions (try to not look at your notes unless you have to). // 1) What is the purpose of the 'this keyword'? - //Answer + //Refers to the newly created object inside a function // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? - //Answer + //Default, Implicit, Explicit, New // 3) What does .bind do? - //Answer + // Sets the context or the value of 'this' keyword //Next Problem @@ -19,23 +19,43 @@ //email --> which is a string //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: 'Bryan', + email: 'dork@gmail.com', + getUsername(){ + return this.username; + } + }; + //Now, invoke the getUsername method and verify you got the username of the object and not anything else. -//Next Problem + user.getUsername() // Write a constructor function, including method definitions, which will make the following function invocations function properly. - //Function Invocations Here - +function Car(brand , model ,year){ + this.brand = brand; + this.model = model; + this.year = year; + + let move = 0; + return { + moveCar(){ + return move + 10; + } + } +} var prius = new Car('Toyota', 'Prius', 2011); var mustang = new Car('Ford', 'Mustang', 2013); + + //Hint, you'll need to add a move property, with a starting value of zero, and write a moveCar function which will increment the move property by 10. The move property will be added to every object that is being returned from the Car function. You'll also need to use the 'this' keyword properly in order to make sure you're invoking moveCar on the right object (prius vs mustang). + prius.moveCar(); //increments prius' move property by 10. Returns the new move property. mustang.moveCar(); //increments mustang' move property by 10. Returns the new move property. @@ -62,23 +82,32 @@ var getYear = function(){ var myUser = { username: 'iliketurtles', age: 13, - email: 'iliketurtles@gmail.com' + email: 'iliketurtles@gmail.com', + + getMyUsername(){ + return this.username; + } + }; var getMyUsername = function() { return this.username; }; -var userName = getMyUsername(); //Fix this + +var userName = myUser.getMyUsername(); //Fix this + +console.log(username); //Above you're given an object, and a function. What will the getMyUsername function return? //Note(no tests) - //Answer Here + // iliketurtles //In the example above, what is the 'this keyword' bound to when getMyUsername runs? - //Answer Here + //username //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..c0e0b19 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Bryan A. Afuente", + "email": "bryan.alfuente@boom.camp" }