Skip to content

Instantly share code, notes, and snippets.

@Alxandr
Created August 27, 2015 03:16
Show Gist options
  • Save Alxandr/6ec89369f6fd51332ee6 to your computer and use it in GitHub Desktop.
Save Alxandr/6ec89369f6fd51332ee6 to your computer and use it in GitHub Desktop.
export function abstract() {
return (cls, prop, descriptor) => {
if (prop === undefined) {
return class Abstract extends cls {
constructor(...args) {
super(...args);
if (this.constructor === Abstract) {
throw new Error('Class is abstract, and should not be instansiated.');
}
}
};
}
function abstract() {
throw new Error(`Method "${prop}" is abstract, and should have been overridden in a subclass.`);
}
if (descriptor.value) {
// this is a method
descriptor.value = abstract;
} else {
// property (getter and/or setter)
if (descriptor.get) {
descriptor.get = abstract;
}
if (descriptor.set) {
descriptor.set = abstract;
}
}
};
}
@abstract()
class Abstract {
@abstract()
get foo() {
}
@abstract()
bar() { }
}
class Actual extends Abstract {
get foo() {
return 'foo';
}
bar() {
return 'bar';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment