Skip to content

Instantly share code, notes, and snippets.

@husa
Last active March 11, 2016 10:16
Show Gist options
  • Save husa/92625b27a3f114910159 to your computer and use it in GitHub Desktop.
Save husa/92625b27a3f114910159 to your computer and use it in GitHub Desktop.
ES7 decorators
{
"presets": ["es2015"],
"plugins": ["transform-decorators-legacy"]
}
  1. clone
  2. npm install
  3. npm run go
class Product {
@insurance
@tax(15)
cost () {
return 100;
}
}
let p = new Product()
console.log(p.cost()) // 165
function insurance (target, key, descriptor) {
const price = descriptor.value();
descriptor.value = () => price + 50;
return descriptor;
}
function tax (taxValue) {
return (target, key, descriptor) => {
const price = descriptor.value();
descriptor.value = () => {
return (1 + taxValue / 100) * price
}
return descriptor;
}
}
{
"name": "Babel-es2016-decorators",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"go": "babel main.js | node"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment