Last active
August 12, 2019 05:47
-
-
Save hirokiky/10de22c2307fdd3b45aed5e7b46216c3 to your computer and use it in GitHub Desktop.
Dataclass like utility.
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 makeModel (fields) { | |
return class BaseModel { | |
constructor(options) { | |
options = options || {} | |
var v; | |
for (var [key, value] of Object.entries(fields)) { | |
if (options.hasOwnProperty(key)) { | |
v = options[key] | |
} else { | |
v = value | |
} | |
if (typeof(v) === "function") { | |
this[key] = v() | |
} else { | |
this[key] = v | |
} | |
} | |
} | |
} | |
} | |
export class User extends makeModel({ | |
id: null, | |
username: null, | |
iconUrl: '' | |
}) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment