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
48 changes: 41 additions & 7 deletions practice.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
//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
//`this` keyword is used to refer to the value of the object inside a function whenever it is invoked. When inside the function,
//it also refers to the value of the windows object whenever a variable is globally declared outside the function.

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

//Answer
//Global binding - this refers to window object or when a function is globally described, `this` keyword inside this function refers to the globla object hence, window object
//Explicit Binding - `call` method pass an explicit reference for the `this` keyword hence, explicit binding. Explicit bindings can also be set using the `apply` method. Hard binding is available in the form of the `bind` method.
//Implicit Binding - when a function is globally described and contained inside one of the property/key inside an Object, this property inside an Object is called Implicit Binding.
//New Binding - creates an object and assign it to another object. If the new object is invoked, it is also equivalent to `.this` keyword

// 3) What does .bind do?

//Answer
//`.bind()` method is a function that calls all properties in an object.
//The value inside the .bind() method is also equivalent to a value that has `this` keyword.
//Before the `.bind()` method is/are the value/s that select which property/ies to be called in an object.


//Next Problem
Expand All @@ -21,21 +27,42 @@

//Code Here

user = {
username: 'franco',
email: 'francisco.ifurung@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 Invocations Here
function Car(type, model, year){
this.type = type;
this.model = model;
this.year = year;
this.move = 0;
}

Car.prototype.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.

Expand All @@ -55,10 +82,13 @@ var getYear = function(){
//Note(no tests)
//Code Here

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

console.log(getPriusYear());
console.log(getMustangYear());

//New Problem

var myUser = {
username: 'iliketurtles',
age: 13,
Expand All @@ -73,12 +103,16 @@ var userName = 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
"windows.username or global var username"


//Fix the getMyUsername invocation (stored in the userName variable, at the bottom of the above code) so that userName will be equal to 'iliketurtles'.

var getMyUsername = function() {
return this.username;
};

var userName = getMyUsername.call(myUser);
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": "Francisco R. Ifurung",
"email": "francisco.ifurung@boom.camp"
}