Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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