Created
February 14, 2012 20:53
-
-
Save petermichaux/1830345 to your computer and use it in GitHub Desktop.
using constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set() { | |
this._elements = []; | |
} | |
Set.prototype.filter = function(fn) { | |
var result = new this.constructor(); | |
for (var i = 0; i < this._elements.length; i++) { | |
if (fn(this._elements[i]) { | |
result.add(this._elements[i]); | |
} | |
} | |
}; | |
// ObservableSet inherits from Set | |
// | |
// Note that calling filter on an observable set will | |
// return an observable set. | |
// | |
function ObservableSet() { | |
Set.apply(this, arguments); | |
} | |
// mixin the addEventListener, removeEventListener, dispatchEvent methods. | |
mixinObservable(ObservableSet.prototype); | |
ObservableSet.prototype.add = function(element) { | |
var modified = Set.prototype.add.call(this, element); | |
if (modifed) { | |
this.dispatchEvent({type:'add', relatedTarget: element}); | |
} | |
return modified; | |
}; | |
ObservableSet.prototype['delete'] = function(element) { | |
var modified = Set.prototype['delete'].call(this, element); | |
if (modifed) { | |
this.dispatchEvent({type:'delete', relatedTarget: element}); | |
} | |
return modified; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment