'This' keyword


#1

Could someone please take a look at my notes here to see if I understood this?

var setAge = function (newAge) {//new function called setAge with parameter newAge
this.age = newAge;//this allows the input of any object with an age property
//and connects it with new Age
};
var bob = new Object();//here we have bob
bob.age = 30;//he's got a property of age with the value of 30

bob.setAge = setAge;//This changes the name of the function to bob.setAge from setAge

bob.setAge(50);//calls the function to change the age of bob


#2

var bob = new Object();//here we have bob
bob.age = 30;//he's got a property of age with the value of 30

So now you have object bob with only one property age.

bob.setAge = setAge;

It means that now bob has method setAge. Now you can use this method and set new age value:

bob.setAge(50);

When you calling this method in this we have bob instance and method knows that we need change value for property age only for current object bob.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.