TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: ES7 Decorator library adds types checking, memoization, and more to JS

66 pointsby mako-tacoalmost 10 years ago

9 comments

jeswinalmost 10 years ago
Correct me if I&#x27;m wrong, but the necessity for decorators seem to have risen from the ES6 class syntax.<p>If we were doing classes&#x2F;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(&quot;number&quot;, &quot;number&quot;, 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.
评论 #9634951 未加载
评论 #9634131 未加载
评论 #9635265 未加载
评论 #9634290 未加载
评论 #9634636 未加载
评论 #9634434 未加载
评论 #9634944 未加载
评论 #9634187 未加载
krat0sprakharalmost 10 years ago
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&#x27;m eager to start using them in JS. Unfortunately, I haven&#x27;t yet come across a good guide till now.
评论 #9634101 未加载
评论 #9635499 未加载
untogalmost 10 years ago
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.
评论 #9634416 未加载
评论 #9634280 未加载
评论 #9634379 未加载
z3t4almost 10 years ago
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&#x27;t lock yourself into classifications. Instead, just check the input and throw an error if it doesn&#x27;t fit.
评论 #9635494 未加载
thomasfoster96almost 10 years ago
Is something similar to this possible with ES6 proxies? It&#x27;d be nice to use a lot of these features outside classes, but since proxies are far harder to polyfill (indeed, I don&#x27;t think anyone&#x27;s some it yet) I&#x27;ve only seen then done with decorators within classes.
评论 #9634162 未加载
评论 #9634134 未加载
评论 #9634103 未加载
评论 #9634128 未加载
aaron-leboalmost 10 years ago
ES&#x2F;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.
skrebbelalmost 10 years ago
This is exactly what I was looking for. Thanks! I can now retire most of my ugly home cooked precondition checking.
salimmadjdalmost 10 years ago
Is there a typo on the Currying example?<p>let addToFiveAndThree = addFive(3);<p>Should be:<p>let addToFiveAndThree = addToFive(3);<p>I belive.
评论 #9634426 未加载
wslhalmost 10 years ago
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.
评论 #9634370 未加载