Created
November 19, 2012 08:00
-
-
Save killwing/4109513 to your computer and use it in GitHub Desktop.
[Registry] a simple registry without moving elements
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
#!/usr/local/bin/node | |
var Registry = function() { | |
this.r = [null]; | |
}; | |
Registry.prototype.register = function(object) { | |
var id = this.r[0]; | |
if (id === null) { | |
this.r.push(object); | |
return this.r.length - 1; | |
} else { | |
this.r[0] = this.r[id]; | |
this.r[id] = object; | |
return id; | |
} | |
}; | |
Registry.prototype.unregister = function(id) { | |
this.r[id] = this.r[0]; | |
this.r[0] = id; | |
}; | |
var reg = new Registry(); | |
reg.register('module1'); | |
var id = reg.register('module2'); | |
reg.register('module3'); | |
reg.unregister(id); | |
reg.register('new_module2'); | |
console.log(reg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment