I'm going through a JavaScript tutorial and I'm able to complete it. But the problem is that I don't understand what one of the lines is doing. I have a function setAge()
and then later after creating a susan
object I set one of the properties to that object as the name of the function? I don't understand why this is done. Wouldn't I be able to use the function/method without doing this?
The tutorial code:
var setAge = function (newAge) {
this.age = newAge;
};
var susan = new Object();
susan.age = 25;
susan.setAge = setAge; //how the hell does this work?
// here, update Susan's age to 35 using the method
susan.setAge(35);