Created
August 18, 2015 15:22
-
-
Save gasp/be505ac8b355901872b1 to your computer and use it in GitHub Desktop.
a prototype-less way to do object with javascript
This file contains hidden or 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
var Collection = function () { | |
return { | |
db: [], | |
add: function (o) { | |
if (typeof(o.uid) === 'undefined') { | |
throw 'error: object has no uid'; | |
} | |
if (this.get(o.uid) === false) { | |
this.db.push(o); | |
} | |
}, | |
get: function (uid) { | |
for (var i = 0; i < this.db.length; i++) { | |
if (this.db[i].uid === uid) return this.db[i]; | |
} | |
return false; | |
}, | |
rem: function (uid) { | |
this.db.splice(this.get(uid), 1); | |
} | |
}; | |
}; | |
var calls = new Collection(); | |
calls.add({ | |
uid: 'a4X2uo0ZxEtkrMGJ', | |
from: 'myriam', | |
to: 'tom' | |
}); | |
console.log(calls); | |
console.log(calls.get('a4X2uo0ZxEtkrMGJ')); | |
calls.rem('a4X2uo0ZxEtkrMGJ'); | |
console.log(calls.get('a4X2uo0ZxEtkrMGJ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment