网络科技

    今日:119| 主题:244924
收藏本版
互联网、科技极客的综合动态。

[其他] Stupid ES6 Tricks

[复制链接]
道听途说 发表于 2016-10-16 07:38:59
196 12

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Stupid ES6 tricks

               
Stupid ES6 Tricks-1 (collecting,techniques,planning,examples,familiar)
             Here are a few ES6 techniques that aren’t really tricks, just exploiting some of the new syntax either to reduce code, improve readability, or possibly just have fun. I’m planning on collecting more here, so please feel free to bookmark and check back every once in a while. Also, if you have some fun tricks that I don’t mention here, please write a response!
    Destructuring

     Destructuring is my hands-down favorite part of ES6 syntax. It’s also in Node 6 now too, so I use it on both the frontend and the backend. Check out Mozilla’s explanation if you’re not familiar with the basic concept, but TL;DR it let’s you assign variables to deeply nested parts of a value easily.
    Simple examples:
    Renaming

     One feature of destructuring you might not know about is renaming while destructuring. If you’ve used ES6 modules, you might know of the as operator for renaming imports and wondered, as I did, why as doesn’t work in destructuring. Well, that’s because destructuring has a different syntax!
    In conjunction with default parameters

     Default parameters let you assign values to parameters that are not passed by the user, but what if you planned to destructure when they do pass in params? No problem… you have a “left-hand side” of an assignment (a.k.a. an lvalue ) with default parameters, so you can use destructuring there too!
    Destructuring itself also has default values, so you can intermix the two!
    Assigns, branches, and leaves

    You can destructure deeply into an object (or array!), but intermediate keys of objects don’t get assigned when you do that. What if you want both the intermediate keys (branch nodes) and some deep node too? Just ask for it! In other words, as we show in the example below, you simply declare any variable you want (intermediate in this case) and use it again as a specification for descending to another level (intermediate -> nested).
    Idiomatic command line arguments for Node

     Node is great for writing scripts. For command line argument you can extract them from process.argv . If you’re doing anything complicated, or really anything intended to be used by humans, you’re best off using something like yargs to parse them. But… if you’ve got a script that simply takes a small number of arguments, you can use array destructuring to skip the first two arguments (usually the node path itself and then the script path) and assign the rest to variables, e.g.
    Enhanced Object Literals

    The upgrades to the object literal syntax are really cool and we used some examples above with the “property value shorthand.”
    The first technique is not a trick at all so much as a way to avoid binding if you don’t need it. Say you want to expose a variable or a function externally, but also want to use it within functions you’re exporting as a utility object. The technique of declaring them outside your returned object and then including them below allows you to avoid binding.
    With JSX

    Ok, so this is not properly ES6, but you can use some ES6 features like property value shorthand very conveniently with JSX spread/rest syntax.
    Object.assign

    Object.assign is a fantastic new addition that’s great for keeping things immutable (or at least only doing ephemeral mutations to intermediate objects). But did you know you can use it for setting array values too?
    What are yours?

    Got any favorite ES6 tricks or techniques that I missed? Respond with them below!
           If you’re interested in working with us, discussing issues like these and others, please check out our  jobs  page and get in touch!
友荐云推荐




上一篇:印度双十一刚过,仍缺少支付宝和三通一达|印度创投周报
下一篇:新手指南:DVWA-1.9全级别教程之Brute Force
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

盼丹 发表于 2016-10-16 08:02:38
不错 支持一个了
回复 支持 反对

使用道具 举报

jinjian1v 发表于 2016-10-16 08:26:32
如果你活着,早晚都会死;如果你死了,你就永远活着
回复 支持 反对

使用道具 举报

无心碍人 发表于 2016-10-16 08:37:32
发贴技术哪家强?道听途说
回复 支持 反对

使用道具 举报

zjwnv 发表于 2016-10-16 08:37:37
沙发抢不到,板凳也行啊!
回复 支持 反对

使用道具 举报

你只是我旳專屬 发表于 2016-10-16 08:37:40
LZ帖子不给力,勉强给回复下吧
回复 支持 反对

使用道具 举报

nunsp 发表于 2016-10-16 08:46:23
如果我做了皇帝,就封你当太子!
回复 支持 反对

使用道具 举报

zaz11888 发表于 2016-10-16 09:02:30
支持,楼下的跟上哈~
回复 支持 反对

使用道具 举报

111111ss 发表于 2016-10-16 09:59:04
纯粹路过,没任何兴趣,仅仅是看在老用户份上回复一下
回复 支持 反对

使用道具 举报

电商小吕哥 发表于 2016-10-17 03:50:21
阅!谢谢分享
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表