Created
November 25, 2012 23:37
-
-
Save ricardobeat/4145887 to your computer and use it in GitHub Desktop.
Private properties with no memory leaks.
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
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