Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Created November 25, 2012 23:37
Show Gist options
  • Save ricardobeat/4145887 to your computer and use it in GitHub Desktop.
Save ricardobeat/4145887 to your computer and use it in GitHub Desktop.
Private properties with no memory leaks.
var Thing = (function(){
var instances = []
, data = []
function setId(){
data[instances.indexOf(this)] = id
}
function getId(){
return data[instances.indexOf(this)]
}
function Thing(id){
instances.push(this)
setId.call(this, id)
}
Thing.prototype.destroy = function(){
var index = instances.indexOf(this)
instances.splice(index, 1)
data.splice(index, 1)
}
Thing.prototype.someMethod = function(){
var id = getId.call(this)
// use id for whatever
}
return Thing
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment