core-decorators.js
Library of ES2016 (ES7) decorators inspired by languages that come with built-ins like @override, @deprecate, etc, similar to pre-defined Annotations in Java. Note that unlike Java annotations, decorators are functions which are applied at runtime.
Get It
A version compiled to ES5 in CJS format is published to npm as core-decorators
npm install core-decorators --save
This form could be consumed by any ES2016 (ES7) transpiler that supports decorators like babel.js with babel --optional es7.decorators,es7.objectRestSpread
or babel --stage 1
or using the recent iterations of TypeScript.
*note that the compiled code is intentionally not checked into this repo
Decorators
Proposed (not implemented, PRs welcome!):
- @mixin
- @instrument/profile
- @debounce
- @throttle
- @private
Docs
@autobind
Forces invocations of this function to always have this
refer to the class instance, even if the function is passed around or would otherwise lose its this
context. e.g. var fn = context.method;
import { autobind } from 'core-decorators';
class Person {
@autobind
getPerson() {
return this;
}
}
let person = new Person();
let getPerson = person.getPerson;
getPerson() === person;
@readonly
Marks a property or method as not being writable.
import { readonly } from 'core-decorators';
class Meal {
@readonly
entree = 'steak';
}
var dinner = new Meal();
dinner.entree = 'salmon';
@override
Checks that the marked method indeed overrides a function with the same signature somewhere on the prototype chain.
Works with methods and getters/setters. Will ensure name, parameter count, as well as descriptor type (accessor/data). Provides a suggestion if it finds a method with a similar signature, including slight misspellings.
import { override } from 'core-decorators';
class Parent {
speak(first, second) {}
}
class Child extends Parent {
@override
speak() {}
}
class Child extends Parent {
@override
speaks() {}
}
@deprecate (alias: @deprecated)
Calls console.warn()
with a deprecation message. Provide a custom message to override the default one. You can also provide an options hash with a url
, for further reading.
import { deprecate } from 'core-decorators';
class Person {
@deprecate
facepalm() {}
@deprecate('We stopped facepalming')
facepalmHard() {}
@deprecate('We stopped facepalming', { url: 'http://knowyourmeme.com/memes/facepalm' })
facepalmHarder() {}
}
let person = new Person();
person.facepalm();
person.facepalmHard();
person.facepalmHarder();
@suppressWarnings
Suppresses any JavaScript console.warn()
call while the decorated function is called. (i.e. on the stack)
Will not suppress warnings triggered in any async code within.
import { suppressWarnings } from 'core-decorators';
class Person {
@deprecated
facepalm() {}
@suppressWarnings
facepalmWithoutWarning() {
this.facepalm();
}
}
let person = new Person();
person.facepalmWithoutWarning();
@nonenumerable
Marks a property or method as not being enumerable.
import { nonenumerable } from 'core-decorators';
class Meal {
entree = 'steak';
@nonenumerable
cost = 20.99;
}
var dinner = new Meal();
for (var key in dinner) {
key;
}
Object.keys(dinner);
@nonconfigurable
Marks a property or method as not being writable.
import { nonconfigurable } from 'core-decorators';
class Meal {
@nonconfigurable
entree = 'steak';
}
var dinner = new Meal();
Object.defineProperty(dinner, 'entree', {
enumerable: false
});
@memoize
Initial implementation included, likely slow. WIP.