From eafaeabce8e7f5e2ffa6143ba3191611f0a115c7 Mon Sep 17 00:00:00 2001 From: vincegludovice <56061835+vincegludovice@users.noreply.github.com> Date: Tue, 15 Oct 2019 14:54:59 +0800 Subject: [PATCH 1/4] Added Javascript this activity --- practice.js | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/practice.js b/practice.js index c5812b5..a171a0b 100644 --- a/practice.js +++ b/practice.js @@ -2,15 +2,18 @@ // 1) What is the purpose of the 'this keyword'? //Answer - + "a shortcut or a way to reference to properties in object" // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? //Answer - + "1) Default binding refers to how this is the global context whenever a function is invoked without any of these other rules." + "2) Implicit binding occurs when dot notation is used to invoke a function." + "3) Explicit binding of this occurs when .call(), .apply(), or .bind() are used on a function." + "4) New Binding has the highest priority" // 3) What does .bind do? //Answer - + "creates new function" //Next Problem @@ -20,21 +23,37 @@ //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: "vinceludovice", + email: "vince.ludovice@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 // Write a constructor function, including method definitions, which will make the following function invocations function properly. - +function Car(brand, model, year){ + this.brand = brand; + this.model = model; + this.year = year; + this.moveCar = function(){ + this.move = 0; + return this.move += 10; + } +} //Function Invocations Here 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). +//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. @@ -54,8 +73,10 @@ var getYear = function(){ //Note(no tests) //Code Here - - +var getPriusYear = getYear.bind(prius), + getMustangYear = getYear.bind(mustang); +console.log(getMustangYear()); +console.log(getPriusYear()); //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 - + "undefined function" //In the example above, what is the 'this keyword' bound to when getMyUsername runs? //Answer Here - +'global window object' //Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'. - +userName(); From 219e2768931a5b6379b13994b4bc2447f1e11afd Mon Sep 17 00:00:00 2001 From: vincegludovice <56061835+vincegludovice@users.noreply.github.com> Date: Tue, 15 Oct 2019 14:55:59 +0800 Subject: [PATCH 2/4] Update user.json --- user.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user.json b/user.json index 4ac80a0..f463234 100644 --- a/user.json +++ b/user.json @@ -1,4 +1,4 @@ { - "name": "", - "email": "" + "name": "Vince Gerard F. Ludovice", + "email": "vince.ludovice@boom.camp" } From eeeb7e7b45507f06e763a329df90c071b933a144 Mon Sep 17 00:00:00 2001 From: vincegludovice <56061835+vincegludovice@users.noreply.github.com> Date: Tue, 15 Oct 2019 16:18:50 +0800 Subject: [PATCH 3/4] Update practice.js --- practice.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/practice.js b/practice.js index a171a0b..d369af3 100644 --- a/practice.js +++ b/practice.js @@ -2,18 +2,18 @@ // 1) What is the purpose of the 'this keyword'? //Answer - "a shortcut or a way to reference to properties in object" +// "a shortcut or a way to reference to properties in object" // 2) What are the four rules that govern what the 'this keyword' is bound to and describe each? //Answer - "1) Default binding refers to how this is the global context whenever a function is invoked without any of these other rules." - "2) Implicit binding occurs when dot notation is used to invoke a function." - "3) Explicit binding of this occurs when .call(), .apply(), or .bind() are used on a function." - "4) New Binding has the highest priority" +// "1) Default binding refers to how this is the global context whenever a function is invoked without any of these other rules." +// "2) Implicit binding occurs when dot notation is used to invoke a function." +// "3) Explicit binding of this occurs when .call(), .apply(), or .bind() are used on a function." +// "4) New Binding has the highest priority" // 3) What does .bind do? //Answer - "creates new function" +// "creates new function" //Next Problem @@ -96,11 +96,11 @@ 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 - "undefined function" +// "undefined function" //In the example above, what is the 'this keyword' bound to when getMyUsername runs? //Answer Here -'global window object' +// 'global window object' //Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'. userName(); From 4c021174db73ee330aa1a2a3274706f2e5baed25 Mon Sep 17 00:00:00 2001 From: vincegludovice <56061835+vincegludovice@users.noreply.github.com> Date: Tue, 15 Oct 2019 16:36:11 +0800 Subject: [PATCH 4/4] Update practice.js --- practice.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/practice.js b/practice.js index d369af3..1adee90 100644 --- a/practice.js +++ b/practice.js @@ -90,17 +90,17 @@ var getMyUsername = function() { return this.username; }; -var userName = getMyUsername.bind(myUser)();//Fix this +var userName = getMyUsername.call(myUser);//Fix this //Above you're given an object, and a function. What will the getMyUsername function return? //Note(no tests) //Answer Here -// "undefined function" + "undefined function" //In the example above, what is the 'this keyword' bound to when getMyUsername runs? //Answer Here -// 'global window object' + 'global window object' //Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'. -userName(); +