From 70cb8fe437802c67bc95976c0440443d1b0389f7 Mon Sep 17 00:00:00 2001 From: Micko Matamorosa Date: Tue, 15 Oct 2019 17:42:14 +0800 Subject: [PATCH] submission --- practice.js | 37 ++++++++++++++++++++++++++++++------- user.json | 4 ++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/practice.js b/practice.js index c5812b5..c2ec825 100644 --- a/practice.js +++ b/practice.js @@ -2,11 +2,14 @@ // 1) What is the purpose of the 'this keyword'? //Answer - + //The purpose of 'this keyword' is a contextual reference to an object. // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? //Answer - + //Implicit binding occurs when dot notation is used to invoke a function. + //Explicit binding occurs when call, apply, or bind are used in a function + //new binding + //Default Binding // 3) What does .bind do? //Answer @@ -20,9 +23,16 @@ //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: 'fresh', + email: 'christian.frecia@boom.camp', + getUsername: function(){ + return this.username; + } + } //Now, invoke the getUsername method and verify you got the username of the object and not anything else. +user.getUsername(); //Next Problem @@ -30,12 +40,22 @@ // Write a constructor function, including method definitions, which will make the following function invocations function properly. //Function Invocations Here - + function Car(make,model,year){ + this.make = make; + this.model = model; + this.year = year; + this.move = 0; + } 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). +Car.prototype.moveCar = function(){ + this.move += 10; + return this.move; +} + 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. @@ -54,7 +74,10 @@ var getYear = function(){ //Note(no tests) //Code Here - + var getPriusYear = getYear.bind(prius); + var getMustangYear = getYear.bind(mustang); + console.log(getPriusYear); + console.log(getMustangYear); //New Problem @@ -74,11 +97,11 @@ var userName = getMyUsername(); //Fix this //Above you're given an object, and a function. What will the getMyUsername function return? //Note(no tests) //Answer Here - + var userName = getMyUsername.bind(myUser)(); //In the example above, what is the 'this keyword' bound to when getMyUsername runs? //Answer Here - + //Global //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..199a4f1 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Christian Ray Frecia", + "email": "christian.frecia@boom.camp" }