Objects1 16/23 Javascript


My code works, I'm just wondering what is the significance of writing this:

bob.setAge = setAge;

The lesson said: "Then when we say bob.setAge = setAge; (line 9), it means whenever we type bob.setAge( ), this.age in the setAge method will refer to bob.age." ...... However, I do not understand. Can someone please further explain this to me? or rephrase it/break it down? here is the full working code.

// 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

Thanks, I appreciate it.


did they mean to say bob.age = setAge ?


or does it mean that

now we will create a function for bob
and so bob.setAge will do what the original setAge function does?


//so bob.setAge = setAge 
//sets bobs function setAge = to the function we made earlier 

var setAge = function (newAge) {
  this.age = newAge;

//I found out through putting the working code from the exercise in node.js in the terminal.

//when i did 
> console.log(bob);
//I got 
{ age: 50, setAge: [Function: setAge] }

//So basically, for line 9 you could have made bobs new function, and named it anything... like  
//bob.newAgeChangingFunction = setAge;


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