https://github.com/aitoroses/js-decorators
A simple transpiler that brings python style decorators to javascript
https://github.com/aitoroses/js-decorators
Last synced: over 1 year ago
JSON representation
A simple transpiler that brings python style decorators to javascript
- Host: GitHub
- URL: https://github.com/aitoroses/js-decorators
- Owner: aitoroses
- Created: 2015-03-15T22:42:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-03-15T23:40:13.000Z (over 11 years ago)
- Last Synced: 2025-01-11T22:30:56.325Z (over 1 year ago)
- Language: JavaScript
- Size: 164 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JS Decorators
A tiny compiler that brings python style decorators to javascript.
## Use
```js
import Compiler from '../lib/compiler';
var compiler = new Compiler();
var result = compiler.compile(source);
```
## Example
```js
// decorators.js
function addAnnotation(decorable, annotation) {
decorable.annotations = decorable.annotations || [];
decorable.annotations.push(annotation);
return decorable;
}
class Annotation1 {}
class Annotation2 {
constructor(config) {
this.config = config;
}
}
export function Decorator1(config, decorable) {
console.log(["Decorator1", config, decorable])
decorable = addAnnotation(decorable, new Annotation1(config))
return decorable;
}
export function Decorator2(config, decorable) {
console.log(["Decorator2", config, decorable])
decorable = addAnnotation(decorable, new Annotation2(config))
return decorable;
}
```
```js
// index.js
import {Decorator1, Decorator2} from './decorators';
@Decorator1
@Decorator2({module: "MY_MODULE"})
class MyClass {
constructor() {
console.log("Instantiate my class");
}
}
MyClass.annotations[0].name == Annotation2 // => true
```