Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Incorporating Joe's suggestion, this is what I eventually ended up with:</p> <pre><code>function subject() { var callbacks = {} this.register = function(name, callback) { callbacks[name] = callback; } this.unregister = function(name) { delete callbacks[name]; } trigger = function() { var a = arguments; var t = this; $.each(callbacks, function(name, callback) { callback.apply(t, a); }); } } //without the following line, 'this' in firefox is 'subject' instead of 'list' (in chrome it is) list.prototype = new subject() //without these, 'list' is not an instanceof 'subject' list.constructor = subject; list.prototype.constructor = list; function list(n) { this.name = n; subject.call(this); //as suggested by Joe var items = {} this.add = function(name, item) { items[name] = item; trigger.call(this, 'add', name); //no way to do this without using call/apply } this.remove = function(name) { delete items[name]; trigger.call(this, 'remove', name); //no way to do this without using call/apply } this.getitems = function() { return items } } //without the following line, 'this' in firefox is 'subject' instead of 'queue' queue.prototype = new subject() //without these, 'queue' is not an instanceof 'subject' queue.constructor = subject; queue.prototype.constructor = queue; function queue(n) { this.name = n; subject.call(this); //as suggested by Joe var items = []; this.enqueue = function(item) { items.push(item); trigger.call(this, 'enqueue', item); //no way to do this without using call/apply } this.dequeue = function() { var d = items.shift(); trigger.call(this, 'dequeue', d); //no way to do this without using call/apply return d; } this.getitems = function() { return items } } var l1 = new list('l1') l1.register('observer1', function() { console.log('l1', this, arguments) }); l1.add('item1', 'value1'); // ^ 'l1', list { name = 'l1' ... }, ['add', 'item1'] var l2 = new list('l2') l2.register('observer2', function() { console.log('l2', this, arguments) }); l2.add('item2', 'value2'); // ^ 'l2', list { name = 'l2' ... }, ['add', 'item2'] var q1 = new queue('q1') q1.register('observer3', function() { console.log('q1', this, arguments) }); q1.enqueue('item3'); // ^ 'q1', queue { name = 'q1' ... }, ['enqueue', 'item3'] console.log(l1 instanceof list, l1 instanceof subject, l1 instanceof queue); // ^ true, true, false console.log(q1 instanceof list, q1 instanceof subject, q1 instanceof queue); // ^ false, true, true </code></pre> <p>This ticks all of my boxes (except for the use of call, but I can live with that).</p> <p>Thanks for all the help,</p> <p>Mattie</p> <p><strong>EDIT: appearantly this does not work as expected. creating a new object overwrites the other objects callbacks</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload