Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Last active August 12, 2019 05:47
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 hirokiky/10de22c2307fdd3b45aed5e7b46216c3 to your computer and use it in GitHub Desktop.
Save hirokiky/10de22c2307fdd3b45aed5e7b46216c3 to your computer and use it in GitHub Desktop.
Dataclass like utility.
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