Please explain the statement


#1

What does "susan.setAge = setAge" mean, where susan is an object and setAge is a function ? Please explain.


#2

Can you provide some code context?

Without that, this almost seems like it would cause a infinite loop if setAge is a function.


#3

This is the 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
bob.setAge(50);


#4

Ah. This is a confusing way for CC to teach -- but here's the gist of what's going on:

  • First you're making the setAge(newAge) function. You see that, yes?
  • Second you define bob as a new object.
  • Third, you're setting bob's age manually to by usage of dot notation to directly access the age field.
  • Fourth (here's where it gets confusing -- sorry!), you're setting a new function in bob, called setAge, to the function defined in the first step, also called setAge. These don't conflict because of the namespace. By adding "bob." before the first setAge, you are changing the scope to bob. That way the interpreter knows not to confuse the local setAge and bob's setAge. Does that make sense? In simple conclusion, you're setting a variable in bob to a function you already created -- they just have the same name!
  • Fifth, you're simply using the new function on bob to re-set his age to 50.

To answer your question, see Fourth above. It should answer your question.

If not, please let me know.


#5

Got it ! Your explanation was awesome. Thank you :smile: