这个前端代码太鬼畜了,完全看不懂了~有大神说一下吗?

2016-12-28 23:09:22 +08:00
 WytheHuang

代码如下: (f =>(x => f(y => x(x)(y)))(x => f(y => x(x)(y))) )(get => node => Array.from(node.childNodes) .filter(node => node.nodeType === Node.ELEMENT_NODE).reduce((depth, node) => Math.max(depth, get(node) + 1), 1) )(document.documentElement)

微博链接: http://weibo.com/1074886493/EodjO45ln?type=comment#_rnd1482937548088

4602 次点击
所在节点    JavaScript
15 条回复
hxsf
2016-12-28 23:53:46 +08:00
手机阅读。没啥格式化工具。

目测是遍历 dom 树 返回最大深度

关键字
箭头函数 立即执行函数
exoticknight
2016-12-29 00:05:47 +08:00
去看看 Y Combinator ,然后应该就懂了……
xcatliu
2016-12-29 00:37:13 +08:00
不知道把代码写成这样有什么意义
alexsunxl
2016-12-29 01:13:07 +08:00
作用就跟一楼说那样了, 获得 dom 树最大深度
其实执行一下就知道了, 一般这种就是从右往左边看
附图
lijsh
2016-12-29 02:29:13 +08:00
美化一下:
(f =>
(x => f(y => x(x)(y)))
(x => f(y => x(x)(y)))
)
(get =>
node =>
Array.from(node.childNodes)
.filter(node => node.nodeType === Node.ELEMENT_NODE)
.reduce((depth, node) => Math.max(depth, get(node) + 1), 1)
)
(document.documentElement)

(f =>
(x => f(y => x(x)(y)))
(x => f(y => x(x)(y)))
)
这种形式是 Y Combinator ,作用是让匿名函数递归自身。

node =>
Array.from(node.childNodes)
.filter(node => node.nodeType === Node.ELEMENT_NODE)
.reduce((depth, node) => Math.max(depth, get(node) + 1), 1)

这里就是找 node 的后代元素嵌套深度的,后面把 document.documentElement 传进来。
lijsh
2016-12-29 02:30:00 +08:00
本想美化一下,没想到排版还是瞎眼…
yidinghe
2016-12-29 09:23:02 +08:00
试下 markdown

```
function(abc) {
alert("Hello!");
}
```
apoclast
2016-12-29 13:10:05 +08:00
注意力从前端挪走几年了. 现在浏览器支持的 javascript 引擎是什么版本? 什么样的浏览器能支持这种函数式的 js 语法?
se77en
2016-12-29 14:01:04 +08:00
@apoclast 主要的浏览器现在都支持了,而且 ES6 规范里规定了 tail call optimization
kopp123
2016-12-29 17:00:16 +08:00
这还算鬼畜。这才叫鬼畜
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+[+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()
lslqtz
2016-12-29 17:06:12 +08:00
@kopp123 居然真的能执行哈哈哈哈哈哈哈
mentalidade
2016-12-29 18:14:38 +08:00
@kopp123 可怕,试了下真可以
KIDJourney
2016-12-29 20:47:40 +08:00
jsfuck
Damn
2016-12-29 23:30:08 +08:00
@kopp123 卧槽,神马原理。。
palmers
2017-01-02 21:54:14 +08:00
箭头函数没有原函数语法清晰 我觉得

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330875

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX