没有什么难度,主要加深对JavaScript的理解
var Student = function (name, no) {
var name = name; //相当于私有属性
var no = no;
this.setName = function (value) {
name = value;
for (var i = 0; i < this.nameModifyListeners.length; i++) {
this.nameModifyListeners[i](this);
}
}
this.getName = function () {
return name;
}
this.setNo = function (value) {
no = value;
}
this.getNo = function () {
return no;
}
this.sayInfo = function () {
alert(name + "," + no);
};
/**
* function(Student)
*/
this.nameModifyListeners = [];
}
var stu = new Student("LYX", "2008");
stu.sayInfo();
stu.nameModifyListeners.push(onNameChange);
stu.setName("LYX2");
function onNameChange(student) {
alert("student info modified!! name:"+student.getName()+" no:"+student.getNo());
}