Just a test <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> Observer Pattern in Javascript </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <mce:script type="text/javascript"><!-- function Observer() { this.update = function() { } } function Student(name, teacher) { Observer.call(this); var _name_ = name; var _teacher_ = teacher; this.update = function() { alert("Student:" + _name_ + " has teacher's new phone number[" + _teacher_.getPhone() + "]"); } this.getName = function() { return _name_; } this.setName = function(name) { _name_ = name; } } function Subject() { this.attach = function(observer) { } this.detach = function(observer) { } this.notice = function() { } } function Teacher() { var _phone_ = ""; var _obs_ = new Array(); this.attach = function(student) { _obs_.push(student); } this.detach = function(student) { _obs_.pop(student); } this.setPhone = function(phone) { _phone_ = phone; this.notice(); } this.getPhone = function() { return _phone_; } this.notice = function() { var ob; for(var i=0; i<_obs_.length; i++) { ob = _obs_[i]; ob.update(); } } this.toString = function() { return "Teacher who has a phone" + _phone_ + ", has " + _obs_.length + " students in class"; } } function Client() { var teacher = new Teacher(); var studentName = ["<a title="" href="http://www2.gliet.edu.cn/gdhq/kudesign/" rel="nofollow" target="_blank">stefli</a>", "Joy" ,"Owen", "Gerrard", "Ronaldo"]; this.main = function() { for(var i=0; i<studentName.length; i++) { var student = new Student(studentName[i], teacher); teacher.attach(student); } teacher.setPhone("1342545674"); } } var client = new Client(); client.main(); // --></mce:script> </head> <body> </body> </html>