Skip to content

Instantly share code, notes, and snippets.

@metanomial
Created December 14, 2019 19:09
Show Gist options
  • Save metanomial/e0c56861af0c3cfaf0268c6aa30bdb82 to your computer and use it in GitHub Desktop.
Save metanomial/e0c56861af0c3cfaf0268c6aa30bdb82 to your computer and use it in GitHub Desktop.
import { UUIDGen } from './utils/UUID.js';
const uuidGen = UUIDGen();
// Object property definition method
class User {
constructor() {
Object.define(this, 'uuid', {
value: uuidGen.next(),
writable: false,
configurable: false
});
}
}
// Private field
class User {
#uuid = uuidGen.next();
get uuid() {
return this.#uuid;
}
}
// Definition shorthand
class User {
uuid := {
value: uuidGen.next(),
writable: false,
configurable: false
}
}
const object = {
a: 5
};
// Object property definition method
Object.defineProperty(object, 'aa', {
get: () => object.a * 2,
set: (value) => object.a = value / 2,
enumerable: false
});
// Definition shorthand
object.aa := {
get: () => object.a * 2,
set: (value) => object.a = value / 2,
enumerable: false
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment