Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
The above code has an error, we cannot access this.observers inside subscribeObserver and other similar methods.
this will be undefined and will give an error { Cannot read property ‘push’ of undefined }
this.observers has issue.
var Subject = function() {
this.observers = []
return {
observers: this.observers,
subscribeObserver: function(observer) {
this.observers.push(observer);
},
unsubscribeObserver: function(observer) {
var index = this.observers.indexOf(observer);
if(index > -1) {
this.observers.splice(index, 1);
}
},
notifyObserver: function(observer) {
var index = this.observers.indexOf(observer);
if(index > -1) {
this.observers[index].notify(index);
}
},
notifyAllObservers: function() {
for(var i = 0; i < this.observers.length; i++){
this.observers[i].notify(i);
};
}
};
};```
Hey, @ayeshamalik8751 and @akinyeleolat, you’re right! Just remove this and should work fine. I’ve made a few changes so it’s less verbose.
const Subject = function () {
let observers = []
return {
subscribe: function (observer) {
observers.push(observer)
},
unsubscribe: function (observer) {
let index = observers.indexOf(observer)
if (index > -1) {
observers.splice(index, 1)
}
},
notify: function (observer) {
let index = observers.indexOf(observer)
if (index > -1) {
observers[index].notify(index)
}
},
broadcast: function () {
for (let i = 0; i < observers.length; i++) {
observers[i].notify(i)
}
},
}
}
const Observer = function () {
return {
notify: function (index) {
console.log('Observer ' + index + ' is notified!')
},
}
}
let subject = new Subject()
let observer = new Observer()
console.log(subject)
console.log(observer)
subject.subscribe(observer)
subject.notify(observer)
subject.broadcast()
We could remove the return statements from our Subject and Observer functions. The main difference is that writing using an explicit return { ... } would expose our methods whereas with class of function constructors it wouldn’t (see my previous comment).
JavaScript is very dynamic and has always more than one way to write the same thing with very subtle diffs. 🤷♂️
const Subject = function () {
let observers = []
this.subscribe = function (observer) {
observers.push(observer)
}
this.unsubscribe = function (observer) {
let index = observers.indexOf(observer)
if (index > -1) {
observers.splice(index, 1)
}
}
this.notify = function (observer) {
let index = observers.indexOf(observer)
if (index > -1) {
observers[index].notify(index)
}
}
this.broadcast = function () {
for (let i = 0; i < observers.length; i++) {
observers[i].notify(i)
}
}
}
const Observer = function () {
this.notify = function (index) {
console.log('Observer ' + index + ' is notified!')
}
}
let subject = new Subject()
let observer = new Observer()
console.log(subject)
console.log(observer)
subject.subscribe(observer)
subject.notify(observer)
subject.broadcast()
Which would be very similar to ES6’s class syntax:
class Subject {
constructor() {
this.observers = []
}
subscribe(observer) {
this.observers.push(observer)
}
unsubscribe(observer) {
let index = this.observers.indexOf(observer)
if (index > -1) {
this.observers.splice(index, 1)
}
}
notify(observer) {
let index = this.observers.indexOf(observer)
if (index > -1) {
this.observers[index].notify(index)
}
}
broadcast() {
for (let i = 0; i < this.observers.length; i++) {
this.observers[i].notify(i)
}
}
}
class Observer {
notify(index) {
console.log('Observer ' + index + ' is notified!')
}
}
let subject = new Subject()
let observer = new Observer()
console.log(subject)
console.log(observer)
subject.subscribe(observer)
subject.notify(observer)
subject.broadcast()