Correct me if I'm wrong, but the necessity for decorators seem to have risen from the ES6 class syntax.<p>If we were doing classes/functions the old imperative style, we could have simply wrapped the RHS in a standard function that does what the decorator does.<p><pre><code> Animal.prototype.speedup = typeCheckDecorator("number", "number", function(x) {
this.speed += x;
return this.speed;
});
</code></pre>
I get that the class syntax makes things easier for people coming from other languages. And maybe makes static analysis easier. But still not sure if it was really needed.
Can someone recommend a simple and easy-to-approach tutorial into ES7 decorators? I have used decorators successfully in my previous Python projects, and I'm eager to start using them in JS. Unfortunately, I haven't yet come across a good guide till now.
Interesting - I wonder whether TypeScript could compile down to this when ES7 is accepted and live in browsers. As it is, I do prefer the syntax of TypeScript.
I used to argue for type decorations, like in most C like languages, but then changed my mind.<p>When programming in a high level language like JavaScript, you should not think about types. Thinking about types will only limit your creativity and problem solving. Instead, you should focus on the logic abstraction.<p>Also you shouldn't lock yourself into classifications. Instead, just check the input and throw an error if it doesn't fit.
Is something similar to this possible with ES6 proxies? It'd be nice to use a lot of these features outside classes, but since proxies are far harder to polyfill (indeed, I don't think anyone's some it yet) I've only seen then done with decorators within classes.
ES/JS continues to get more and more pythonic. Unless you really need a certain library (which is a really good reason), it is getting harder to pick Python over Node for new projects.
It seems like dynamic programming languages end up implementing decorators. To specify types it would be better to specify them in the parameters instead of adding a few lines.