{"size": 1326, "ext": "js", "max_stars_repo_path": "Behavioral/Observer/JavaScript/observer.js", "max_stars_repo_name": "jerryshueh/design-patterns", "max_stars_repo_stars_event_min_datetime": "2017-10-14T01:18:09.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T12:11:27.000Z", "max_issues_repo_path": "Behavioral/Observer/JavaScript/observer.js", "max_issues_repo_name": "dsfb/design-patterns", "max_issues_repo_issues_event_min_datetime": "2017-10-14T10:06:36.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-05T07:12:01.000Z", "max_forks_repo_path": "Behavioral/Observer/JavaScript/observer.js", "max_forks_repo_name": "dsfb/design-patterns", "max_forks_repo_forks_event_min_datetime": "2017-10-14T10:27:58.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-13T11:51:29.000Z"}
{"max_stars_count": 294, "max_issues_count": 68, "max_forks_count": 329, "avg_line_length": 22.8620689655, "max_line_length": 61, "alphanum_fraction": 0.6983408748}
// Setup Subject
var Subject = function() {
  this.observerList = [];
};

Subject.prototype.subscribeObserver = function(obj) {
  this.observerList.push(obj);
};

Subject.prototype.unsubscribeObserver = function(obj) {
  var index = this.observerList.indexOf(obj);
  if (index > -1) {
    this.observerList.splice(index, 1);
  }
};

Subject.prototype.notifyObserver = function(obj) {
  var index = this.observerList.indexOf(obj);
  if (index > -1) {
    this.observerList[index].notify(index);
  }
};

Subject.prototype.notifyAllObservers = function() {
  for (var i = 0; i < this.observerList.length; i++) {
    this.observerList[i].notify(i);
  }
};

// Setup Observer
var Observer = function() {
  return {
    notify: function(index) {
      console.log("Observer " + index + " is notified!");
    }
  };
};

var subject = new Subject();

var observer1 = new Observer();
var observer2 = new Observer();
var observer3 = new Observer();
var observer4 = new Observer();

subject.subscribeObserver(observer1);
subject.subscribeObserver(observer2);
subject.subscribeObserver(observer3);
subject.subscribeObserver(observer4);

subject.notifyObserver(observer2); // Observer 1 is notified!

subject.notifyAllObservers();
// Observer 0 is notified!
// Observer 1 is notified!
// Observer 2 is notified!
// Observer 3 is notified!
