Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions practice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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);
Expand All @@ -53,8 +75,7 @@ var getYear = function(){


//Note(no tests)
//Code Here

//Code Here


//New Problem
Expand All @@ -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'.

4 changes: 2 additions & 2 deletions user.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "",
"email": ""
"name": "Trizha Kate N. Longaza",
"email": "trizha.kate@boom.camp"
}