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
39 changes: 33 additions & 6 deletions practice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
// 1) What is the purpose of the 'this keyword'?

//Answer
//The purpose of the 'this keyword' is to refer the object where it belongs to.

// 2) What are the four rules that govern what the 'this keyword' is bound to and describe each?

//Answer
//The four rules that govern the 'this keyword' are, first if the new keyword is used when calling the function, this inside the function is a brand new object. Second, if apply, call, or bind are used to call a function, this inside the function is the object that is passed in as the argument. Third, if a function is called as a method — that is, if dot notation is used to invoke the function — this is the object that the function is a property of. In other words, when a dot is to the left of a function invocation, this is the object to the left of the dot. Last, if a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it’s window.



// 3) What does .bind do?

//Answer
//Bind creates a new function.


//Next Problem
Expand All @@ -20,6 +25,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: 'diana',
email : 'diana@boom.camp',
getUsername (){
return this.username;
}
}

//Now, invoke the getUsername method and verify you got the username of the object and not anything else.

Expand Down Expand Up @@ -54,31 +66,46 @@ var getYear = function(){

//Note(no tests)
//Code Here
function Car(make, model, year, move){
this.make = make;
this.model = model;
this.year = year;
this.move = 0;
return {
moveCar () {
this.move +=10;
return this.move;
}
}
}


getPriusYear = getYear.bind(prius);
getMustangYear = getYear.bind(mustang);


//New Problem

var myUser = {
username: 'iliketurtles',
age: 13,
email: 'iliketurtles@gmail.com'
};

var getMyUsername = function() {
email: 'iliketurtles@gmail.com',
getMyUsername : function() {
return this.username;
}
};

var userName = getMyUsername(); //Fix this
var userName = myUser.getMyUsername(); //Fix this

//Above you're given an object, and a function. What will the getMyUsername function return?
//Note(no tests)
//Answer Here
//undefined

//In the example above, what is the 'this keyword' bound to when getMyUsername runs?

//Answer Here

//none

//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": "Diana Lyn C. Geromo",
"email": "diana.geromo@boom.camp"
}