Social Icons

Pages

Thursday, December 1, 2016

Introduction to Objects I 16/33

The "this" Keyword
Our setAge method works great for bobbecause it updates bob.age, but what if we want to use it for other people?
It turns out we can make a method work for many objects using a new keyword, this. The keyword this acts as a placeholder, and willrefer to whichever object called that method when the method is actually used.
Let's look at the method setAge (line 2) to see how this works. By using the keyword this,setAge will change the age property of any object that calls it. Previously, we had a specific object bob instead of the keywordthis. But that limited the use of the method to just bob.
Then when we say bob.setAge = setAge; (line 9), it means whenever we type bob.setAge( ),this.age in the setAge method will refer tobob.age.


Instructions
To show this way of making setAge works just like the one in exercise 2, use bob's setAgemethod to change his age to 50.

// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;
// change bob's age to 50 here
bob.setAge(50);

No comments:

Post a Comment