有没有好的 JS 速记参考?
我希望将任何速记技术融入我的常规编码习惯中,并且当我在紧凑的代码中看到它们时也能够阅读它们。
有人知道概述技术的参考页或文档吗?
编辑:我之前提到过缩小器,现在我很清楚缩小和高效的 JS 键入技术是两个几乎完全独立的概念。
I'd like to incorporate whatever shorthand techniques there are in my regular coding habits and also be able to read them when I see them in compacted code.
Anybody know of a reference page or documentation that outlines techniques?
Edit: I had previously mentioned minifiers and it is now clear to me that minifying and efficient JS-typing techniques are two almost-totally-separate concepts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
已更新,添加了 ECMAScript 2015 (ES6) 好东西。请参阅底部。
最常见的条件简写是:
用于创建对象和数组的对象文字表示法:
内置类型(数字、字符串、日期、布尔值)
变量声明:
索引处的字符串字符:
ECMAScript 2015 (ES6) 标准简写
这些是相对较新的添加内容,因此请勿使用预计不会得到浏览器的广泛支持。
它们可能受到现代环境(例如:较新的node.js)或通过转译器的支持。当然,“旧”版本将继续有效。
箭头函数
其余参数
默认参数值
解构
对象字面量内的方法定义
对象字面量内的计算属性名称
奖励:内置对象上的新方法
奖励2:箭头函数也使
self = this
捕获不必要的关于类型的最后说明
请小心使用隐式和隐式类型。隐藏类型转换和舍入,因为它可能会导致代码可读性较差,并且其中一些代码不受现代 Javascript 样式指南的欢迎。
但即使是那些比较晦涩难懂的代码也有助于理解其他人的代码,阅读最小化的代码。
Updated with ECMAScript 2015 (ES6) goodies. See at the bottom.
The most common conditional shorthands are:
Object literal notation for creating Objects and Arrays:
Built in types (numbers, strings, dates, booleans)
Variable declaration:
String's character at index:
ECMAScript 2015 (ES6) standard shorthands
These are relatively new additions so don't expect wide support among browsers.
They may be supported by modern environments (e.g.: newer node.js) or through transpilers. The "old" versions will continue to work of course.
Arrow functions
Rest parameters
Default parameter values
Destructuring
Method definition inside object literals
Computed property names inside object literals
Bonus: new methods on built-in objects
Bonus 2: arrow functions also make
self = this
capturing unnecessaryFinal note about types
Be careful using implicit & hidden type casting and rounding as it can lead to less readable code and some of them aren't welcome by modern Javascript style guides.
But even those more obscure ones are helpful to understand other people's code, reading minimized code.
如果通过 JavaScript 您还包含比 1.5 更新的版本,那么您还可以看到以下内容:
表达式闭包:
JavaScript 1.7 及更早版本:
JavaScript 1.8 添加了简写 Lambda 表示法,用于使用 表达式闭包:
reduce()方法:
JavaScript 1.8还引入了reduce() 数组方法:
解构赋值:
在 JavaScript 1.7 中,您可以使用 解构赋值,例如,交换值以避免临时变量:
数组推导式和 filter() 方法:
< JavaScript 1.7 中引入了 a href="https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects#Array_compressiveons">数组推导式,它可以减少以下代码:
像这样:
或者使用数组中的
filter()
方法在JavaScript 1.6:If by JavaScript you also include versions newer than version 1.5, then you could also see the following:
Expression closures:
JavaScript 1.7 and older:
JavaScript 1.8 added a shorthand Lambda notation for writing simple functions with expression closures:
reduce() method:
JavaScript 1.8 also introduces the reduce() method to Arrays:
Destructuring assignment:
In JavaScript 1.7, you can use the destructuring assignment, for example, to swap values avoiding temporary variables:
Array Comprehensions and the filter() method:
Array Comprehensions were introduced in JavaScript 1.7 which can reduce the following code:
To something like this:
Or using the
filter()
method in Arrays which was introduced in JavaScript 1.6:您正在寻找 JavaScript 语言的习语。
http://ajaxian.com/archives/javascript-idioms-you-need -了解
看看 JavaScript 1.6+ 中的新功能确实很有趣,但你不知道由于缺乏主流支持,将能够在野外使用语言功能(例如,列表推导式或
yield
关键字)。然而,如果您还没有接触过 Lisp 或 Scheme,那么学习新的标准库函数是值得的。许多典型的函数式编程,例如 map、reduce 和 filter 很值得了解,并且经常出现在 jQuery 等 JavaScript 库中;另一个有用的函数是 bind (代理 在 jQuery 中,在一定程度上),这在将方法指定为回调时很有帮助。You're looking for idioms of the JavaScript language.
http://ajaxian.com/archives/javascript-idioms-you-need-to-know
It's certainly interesting to peek at what's new in JavaScript 1.6+ but you're not going to be able to use the language features (e.g., list comprehensions or the
yield
keyword) in the wild due to lack of mainstream support. It is worthwhile to learn about new standard library functions if you haven't had exposure to Lisp or Scheme, however. Many typical pieces of functional programming such as map, reduce, and filter are good to know and often show up in JavaScript libraries like jQuery; another useful function is bind (proxy in jQuery, to an extent), which is helpful when specifying methods as callbacks.获取数组的最后一个值
这并不是真正的简写,而更像是大多数人使用的技术的更短的替代技术
当我需要获取数组的最后一个值时,我通常使用以下技术:
< 的部分code>.slice(-1)[0] 是速记技术。与我看到的几乎每个人都使用的方法相比,这更短:
测试这个简写的相对计算速度
为了测试这一点,我做了以下操作:
然后分别(以防止可能的同步运行):
结果:
结论: 没有缺点使用这个简写。
调试简写
大多数浏览器确实支持每个具有 id 的元素的隐藏全局变量。因此,如果我需要调试某些内容,我通常只需向元素添加一个简单的 id,然后使用我的控制台通过全局变量访问它。您可以自己检查一下:只需在此处打开控制台,输入
footer
并按 Enter 键。它很可能会返回如果全局变量已经是由其他变量占用我通常使用可怕
document.all['idName']
或document.all.idName
。当然知道这已经非常过时了,我不会在任何实际脚本中使用它,但当我真的不想输入完整的document.getElementById('idName')< 时,我会使用它/code> 因为大多数浏览器都支持它,是的,我确实很懒。
Getting the last value of an array
This is not really a shorthand, but more like a shorter alternative technique to the technique most people use
When I need to get the last value of an array, I usually use the following technique:
The part of
.slice(-1)[0]
being the shorthand technique. This is shorter compared to the method I've seen almost everyone else use:Testing this shorthand's relative computing speed
To test this, I did the following:
And then seperately (to prevent possible synchronous running):
The results:
Conclusion: No disadvantages in using this shorthand.
Debugging shorthands
Most browsers do support hidden global variables for every element with an id. So, if I need to debug something, I usually just add a simple id to the element and then use my console to access it via the global variable. You can check this one yourself: Just open your console right here, type
footer
and press enter. It will most likely return the<div id="footer>
unless you've got that rare browser that doesn't have this (I haven't found any).If the global variable is already taken up by some other variable I usually use the horrible
document.all['idName']
ordocument.all.idName
. I am of course aware this is terribly outdated, and I don't use it in any of my actual scripts, but I do use it when I don't really want to type out the fulldocument.getElementById('idName')
since most browsers support it anyway, Yes I am indeed quite lazy.这个 github 存储库致力于 Javascript 的字节节省技术。我觉得非常方便!
https://github.com/jed/140bytes/wiki/Byte- saving-techniques
This github repo is dedicated to byte-saving techniques for Javascript. I find it quite handy!
https://github.com/jed/140bytes/wiki/Byte-saving-techniques