TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Fat arrow functions in Javascript

47 点作者 vbv超过 11 年前

13 条评论

graue超过 11 年前
Did anyone else do a double take reading this?<p>&gt; <i>If you’re a JavaScript programmer, chances are you’ve seen (and done) something like this before:</i><p><pre><code> var listener = node.addEventListener(&quot;click&quot;, function(event) { let _target = event.target; this.handleClick(_target); }.bind(this)); </code></pre> I had to go look up `let` in JavaScript[1]. It appears to still be a bleeding-edge feature not widely supported[2] outside of Firefox... not even in Node with the --harmony flag. I wonder if this is meant to be subtle pro-ES6 propaganda, or the author really takes `let` for granted and doesn&#x27;t realize most JS programmers have never seen it :)<p>1. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>2. <a href="http://kangax.github.io/es5-compat-table/es6/#let" rel="nofollow">http:&#x2F;&#x2F;kangax.github.io&#x2F;es5-compat-table&#x2F;es6&#x2F;#let</a>
评论 #6419935 未加载
评论 #6420274 未加载
ender7超过 11 年前
<i>First and probably most useful is that they gain the lexical scope of the environment they’re defined in.</i><p>Nit: technically, all Javascript functions are lexically scoped. The fat arrow permanently binds the value of &quot;this&quot;, which is sort of like modifying the scope, but again not technically because &quot;this&quot; is a special keyword and not a variable.
评论 #6419255 未加载
评论 #6420333 未加载
评论 #6418722 未加载
tieTYT超过 11 年前
This article is written for someone who already understands the old issue that this is fixing (there&#x27;s nothing wrong with writing it like that). But for someone like me that doesn&#x27;t understand when you need to use this `bind()` stuff and the context your function is in (if I&#x27;m even saying that correctly), what can I read to get a better understanding of what&#x27;s going on here?<p>In other words, what do I need to read before I can understand this article?
评论 #6419772 未加载
评论 #6419362 未加载
评论 #6419368 未加载
EGreg超过 11 年前
I am not pleased at seeing language bloat. Look at what happened with C++11. Languages are supposed to give a common base for developers to read and write the same code. New developers should be able to get up to speed quickly, and see the intent of the other developers, which is one of the reasons Linux is written in C instead of C++.<p><i>&quot;Look at all that saved typing!&quot;</i><p>Yeah, shockingly there were only a few characters saved. And yet the mental overhead of a whole new syntax is introduced, which developers can now encounter in the wild.<p><i>&quot;The real benefit of course is that you don’t have to go through the mental hoop-jumping of trying to figure out what scope your function is going to run in (and more often-than-not, you just wanted it to run inside the current scope the function is being defined in anyway).&quot;</i><p>The amount of mental hoop-jumping recalling more language features and what they do outweighs this. I can understand if something is really used all the time. But when something is already a pattern that&#x27;s pretty straightforward to type, do we really need another lexical element, which differs in obscure semantic details like &quot;you can&#x27;t override the this variable&quot; and other things?<p>I would argue that languages which are &quot;easy to learn, tough to master&quot;, like Chess, are best for programming large projects.
评论 #6419894 未加载
评论 #6419402 未加载
评论 #6419629 未加载
评论 #6419326 未加载
kurrent超过 11 年前
This will be a much needed and welcome addition to ECMA 6.<p>If you&#x27;re interested in a bit more of a comprehensive overview, Nicholas Zakas previously wrote an excellent article on arrow functions :<p><a href="http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/" rel="nofollow">http:&#x2F;&#x2F;www.nczonline.net&#x2F;blog&#x2F;2013&#x2F;09&#x2F;10&#x2F;understanding-ecmas...</a>
crazygringo超过 11 年前
So let me get this straight... a whole new function syntax is being introduced, just to replace:<p><pre><code> var that = this; var f = function() { return that.x; } </code></pre> with:<p><pre><code> var f = () =&gt; { return this.x; } </code></pre> I mean, that&#x27;s it? That&#x27;s the whole benefit? Am I missing something else here? Please tell me I am.<p>JavaScript is already tricky enough to keep track of everything having to do with &quot;this&quot;, now they&#x27;re adding extra complexity to that too by having multiple types of functions that treat &quot;this&quot; even <i>more</i> differently? (Since it&#x27;s not like they&#x27;re removing the original behavior...)
jwoah12超过 11 年前
Is this syntax part of ECMAScript 6? I.E. will it eventually be supported by all browsers?
评论 #6420520 未加载
评论 #6418587 未加载
gwwar超过 11 年前
While this is pretty cool, I&#x27;m not really a fan of this. There is less to type, but I end up spending more time reading what the code is doing since there&#x27;s more than one way to do it. (With optional parens, optional braces, and special syntax for returning objects in a single line).<p>I would have preferred a different function keyword that had lexical this binding.
评论 #6419052 未加载
asaarinen超过 11 年前
Frankly the fat arrow syntax adds so little value that it&#x27;s not worth adding new syntax to JavaScript, just my humble opinion.<p>The fat arrow seems to be primarily about easier scoping when using this - so it&#x27;s trying to patch the single most broken feature a language ever had.<p>The existing function syntax is fine, rather just avoid using &quot;this&quot; at all in your code.
doublerebel超过 11 年前
This pattern is incredibly useful, once I had it available I started seeing it constantly. Particularly for binding functions to events while retaining context -- very common in JavaScript.<p>Knowing the value of `this` at the <i>beginning</i> of a function makes for much more readable code. Fat arrow and all the other ES6 features available in CoffeeScript and Traceur are now too valuable to live without imho.
jmcdonald-ut超过 11 年前
Is there a reason why the author is referring to them as fat arrow functions instead of lambdas? I am not trying to be snarky, this is genuine curiosity if there is something differentiating.
评论 #6420630 未加载
Touche超过 11 年前
Wouldn&#x27;t it be:<p><pre><code> let x = (...args) =&gt; { &#x2F;* some function gunk *&#x2F; }; </code></pre> So I wonder if this is allowed:<p><pre><code> let x = ...args =&gt; args.join(&#x27;,&#x27;)</code></pre>
评论 #6419741 未加载
smrtinsert超过 11 年前
Having seen all the coffeescript pushback here already I&#x27;m not surprised at the pushback here now. The truth is, it takes maybe a day to get really comfortable with using the fat arrow, and after that you wonder why it was any other way before.<p>Sometimes I wonder if a lot of the anti-coffeescript people in this forum are simply Javascript native programmers and don&#x27;t want any change period. I can&#x27;t think of many programming languages that stop changing - seems to be the norm.