Skip to content

Instantly share code, notes, and snippets.

@igormukhin
Created September 20, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igormukhin/7ec738764141d4443e76415178075a5a to your computer and use it in GitHub Desktop.
Save igormukhin/7ec738764141d4443e76415178075a5a to your computer and use it in GitHub Desktop.
Mimics Java Set in JavaScript (DRAFT, NOT TESTED)
function Set() {
this.data = {};
this.add.apply(this, arguments);
}
Set.prototype = {
add: function (key) {
this.data[key] = true;
},
remove: function (key) {
delete this.data[key];
},
clear: function () {
this.data = {};
return this;
},
contains: function (key) {
return this.data.hasOwnProperty(key);
},
isEmpty: function () {
for (var key in this.data) {
if (this.has(key)) {
return false;
}
}
return true;
},
size: function () {
},
keys: function () {
var results = [];
$.each(this.data, function (idx, key) {
results.push(key);
});
return results;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment