16. This


#1

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

I understand the above code, but what the ■■■■ is that line:

bob.setAge = setAge;

???


#2

bob.setAge = setAge;

that is a redundant statement. I believe that the rest of your code is alright as is.

Is there a specific question you have about this lesson? Otherwise, please refrain from putting profanity in the forums, even if it automatically redacts the response.


#3

Lolman, "bob.setAge = setAge;" is NOT redundant. It is completely necessary for the code to properly execute.
If you didn't have that line, then the function "bob.setAge" would be undefined. By typing "bob.setAge =" you create a method for bob, but it is undefined. So, you point it to the "setAge" function.

Defining setAge and then saying "bob.setAge = setAge;" is the same as saying:

bob.setAge = function (newAge) {
this.age = newAge;
}

So, no, it's not redundant.


#4

Pyrockstar10844, read my message to lolman. It defines the function "bob.setAge" as the setAge function. It is NOT redundant.


#5

The method must be passed to the new object if you want to use it within the object itself.
You can name the inherited method by the object to whatever you want.
It doesn't have to be the same name as the original method name.

...
susan.nameItWhatEverYouWant = setAge;
susan.nameItWhatEverYouWant(35); 
...

#6

Thanks, now it's a little bit more clear


#7

thanks, it's work for me.