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
Expand Up @@ -2,15 +2,24 @@
// 1) What is the purpose of the 'this keyword'?

//Answer
// Means the owner of the function

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

//Answer
// 1- Default Binding: 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.
// whatever is to the left of the dot becomes the context for this in the function.
// 3- Explicit Binding: occurs when .call(), .apply(), or .bind() are used on a function.
// 4- New Binding: referencing that new object that the interpretor created


// 3) What does .bind do?

//Answer

// creates a new function that, when called, has its this keyword set to the provided value,
// with a given sequence of arguments preceding any provided when the new function is called.

//Next Problem

Expand All @@ -20,27 +29,47 @@
//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 = new Object({
username : 'Micko Matamorosa',
email : 'micko.matamorosa@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(brand,carname,year){
this.brand = brand;
this.carname = carname;
this.year = year;
this.add = 0;
this.move = 0;
}

Car.prototype.moveCar = function(move){
return this.move += 10;
}

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.



//Continuation of previous problem

var getYear = function(){
Expand All @@ -55,7 +84,8 @@ var getYear = function(){
//Note(no tests)
//Code Here


var priusYear = getYear.bind(prius);
var mustangYear = getYear.bind(mustang);

//New Problem

Expand All @@ -69,16 +99,20 @@ 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
// I think, it getMyUsername returns undefined but if we allow to be assigned and call to myUser object using call() method
// it will return 'iliketurtles' which is the value of myUser.username

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

//Answer Here

// window - if not binded to myUser object
// myUser - if binded to myUser 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'.


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": "Micko Matamorosa",
"email": "micko.matamorosa@boom.camp"
}