From a43130e50189155692169b3266a223549b38ad26 Mon Sep 17 00:00:00 2001 From: Trizha Kate Longaza Date: Mon, 27 May 2019 14:19:19 +0800 Subject: [PATCH 1/2] javascript-3-this: completed --- practice.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/practice.js b/practice.js index c5812b5..6ad30a4 100644 --- a/practice.js +++ b/practice.js @@ -2,14 +2,20 @@ // 1) What is the purpose of the 'this keyword'? //Answer + //When a function is called with the new operator, 'this' refers to the newly created object inside that function. // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? //Answer + //- Implicit Binding - this happens when dot notation is used to invoke a function + //- Explicit Binding - this happens when call, apply, or bind are used on a function + //- new binding - this has the highest priority + //- Default Binding -the most common case of function calls the standalone function invocation // 3) What does .bind do? //Answer + //The bind method allows you to attach an object to a function and then reference the attached object using the 'this' keyword //Next Problem @@ -20,6 +26,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: 'trizhaxkate', + email: 'trizha.longaza@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. @@ -30,6 +43,15 @@ // Write a constructor function, including method definitions, which will make the following function invocations function properly. //Function Invocations Here +function Car(brand, model, year, move){ + this.brand = brand; + this.model = model; + this.year = year; + this.move = 0; +} +Car.prototype.moveCar = function() { + return this.move += 10; +} var prius = new Car('Toyota', 'Prius', 2011); var mustang = new Car('Ford', 'Mustang', 2013); @@ -53,8 +75,7 @@ var getYear = function(){ //Note(no tests) - //Code Here - + //Code Here //New Problem @@ -69,16 +90,17 @@ var getMyUsername = function() { return this.username; }; -var userName = getMyUsername(); //Fix this +var userName = getMyUsername.bind(myUser)(); //Fix this //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 - + //myUser //Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'. From c7c25f038ada39f4ab03dc3a754a1f8120890c9c Mon Sep 17 00:00:00 2001 From: Trizha Kate Longaza Date: Mon, 27 May 2019 14:20:51 +0800 Subject: [PATCH 2/2] modified user.json --- user.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user.json b/user.json index 4ac80a0..0f44441 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Trizha Kate N. Longaza", + "email": "trizha.kate@boom.camp" }