0

In the following DOM event handling example (using jQuery):

$('#shopping-cart').on('click', function(event){...});

could I not call the shopping-cart DOM element the observable, and the function the observer? There is no "general" click event being hooked to, but a click event specific to the shopping-cart object itself.

The javascript example of observable/observer example I'm operating from is the following (taken from here):

// MyFancyObservable.js
var util = require('util'); 
var EventEmitter = require('events').EventEmitter;
function MyFancyObservable() { 
 EventEmitter.call(this);
}
util.inherits(MyFancyObservable, EventEmitter);
MyFancyObservable.prototype.hello = function (name) { 
 this.emit('hello', name);
};
var MyFancyObservable = require('MyFancyObservable'); 
var observable = new MyFancyObservable();
observable.on('hello', function (name) { 
 console.log(name);
});
observable.hello('john'); 

Notice that in the MyFancyObservable example, the on('hello', function) looks exactly like DOM event registration.

asked Aug 5, 2016 at 21:16

1 Answer 1

1

The difference lies in the purpose of the Event.

In your Shopping Cart example, the event gets fired in response to a user action.

In an Observable, the event gets fired in response to a state change.

Are they both examples of Event Handling? Yes.

Are they both Observers? No.

answered Aug 5, 2016 at 21:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.