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
50 changes: 37 additions & 13 deletions practice.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
//We're in a job interview. Answer the following questions (try to not look at your notes unless you have to).
// 1) What is the purpose of the 'this keyword'?

//Answer
//The keyword 'this' is used in JavaScript to point to a particular object.

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

//Answer
/*
1. Global Scope - if a function which includes 'this' keyword, is called from the global scope then this will point to the window object.
2. Implicit Binding - this points to the object on which the function is called (what’s to the left of the period when the function is called).
3. Explicit Binding - explicitly tell the JavaScript engine to set this to point to a certain value using call, apply or bind.
4. new Binding - using the new keyword constructs a new object, and this points it.

*/

// 3) What does .bind do?

//Answer
//It can be used to set the context of 'this' to a specified object when a function is invoked.*/


//Next Problem
//Next Problem

//Create an object called user which has the following properties.
//username --> which is a string
//email --> which is a string
//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: "jericho",
email: "jericho.aldemo@boom.camp",
getUsername() {
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 Invocations Here
function Car(brand, model, year) {
this.move = 0
this.brand = brand
this.model = model
this.year = year
this.moveCar = function() {
this.move += 10
return this.move
}
}

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).

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.
console.log(prius.moveCar()); //increments prius' move property by 10. Returns the new move property.
console.log(mustang.moveCar()); //increments mustang' move property by 10. Returns the new move property.



Expand All @@ -51,9 +73,11 @@ var getYear = function(){
// Then use the bind function to bind the mustang to the function and save it in a varabile called getMustangYear
// Console Log the results of the getPriusYear and getMustangYear to see that they are returning the correct years.


//Note(no tests)
//Code Here
var getPriusYear = getYear.bind(prius)
var getMustangYear = getYear.bind(mustang)
console.log(getPriusYear())
console.log(getMustangYear())



Expand All @@ -69,15 +93,15 @@ var getMyUsername = function() {
return this.username;
};

var userName = getMyUsername(); //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

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

//Answer Here
//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'.
Expand Down
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": "Jericho Aldemo",
"email": "jericho.aldemo@boom.camp"
}