我正在学习JavaScript教程,已经完成了其中的一个部分。但是问题是我不理解其中一行代码的作用。
我有一个名为setAge()
的函数,在创建了一个susan
对象后,我将该对象的一个属性设置为该函数的名称?我不明白为什么要这样做。难道我不能在不这样做的情况下使用该函数/方法吗?
教程中的代码:
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);