Created
September 13, 2012 18:58
-
-
Save rwaldron/3716743 to your computer and use it in GitHub Desktop.
Using a WeakMap to store instance data properties
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
(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