Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created September 13, 2012 18:58
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 rwaldron/3716743 to your computer and use it in GitHub Desktop.
Save rwaldron/3716743 to your computer and use it in GitHub Desktop.
Using a WeakMap to store instance data properties
(function( exports ) {
var records = new WeakMap();
function Record( data ) {
records.set(this, data);
}
Record.prototype.save = function() {
... persist the things
};
Record.prototype.get = function( field ) {
return records.get(this)[ field ];
};
Record.prototype.set = function( field, value ) {
var data = records.get(this);
data[ field ] = value;
records.set(this, data);
};
exports.Record = Record;
}(this));
var rec = new Record({ save: "no problem!" });
// good to go :D
rec.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment