{"action":"created","issue":{"url":"https://api.github.com/repos/jawil/blog/issues/24","repository_url":"https://api.github.com/repos/jawil/blog","labels_url":"https://api.github.com/repos/jawil/blog/issues/24/labels{/name}","comments_url":"https://api.github.com/repos/jawil/blog/issues/24/comments","events_url":"https://api.github.com/repos/jawil/blog/issues/24/events","html_url":"https://github.com/jawil/blog/issues/24","id":251462197,"number":24,"title":"这些JavaScript编程黑科技，装逼指南，高逼格代码，让你惊叹不已","user":{"login":"jawil","id":16515708,"avatar_url":"https://avatars0.githubusercontent.com/u/16515708?v=4","gravatar_id":"","url":"https://api.github.com/users/jawil","html_url":"https://github.com/jawil","followers_url":"https://api.github.com/users/jawil/followers","following_url":"https://api.github.com/users/jawil/following{/other_user}","gists_url":"https://api.github.com/users/jawil/gists{/gist_id}","starred_url":"https://api.github.com/users/jawil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jawil/subscriptions","organizations_url":"https://api.github.com/users/jawil/orgs","repos_url":"https://api.github.com/users/jawil/repos","events_url":"https://api.github.com/users/jawil/events{/privacy}","received_events_url":"https://api.github.com/users/jawil/received_events","type":"User","site_admin":false},"labels":[{"id":553638914,"url":"https://api.github.com/repos/jawil/blog/labels/JS","name":"JS","color":"ff6699","default":false},{"id":553642980,"url":"https://api.github.com/repos/jawil/blog/labels/%E7%8E%84%E5%AD%A6","name":"玄学","color":"00ee88","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":50,"created_at":"2017-08-20T02:40:12Z","updated_at":"2017-08-22T03:49:09Z","closed_at":null,"body":"[![](https://badge.juejin.im/entry/5998f8396fb9a0247c6ec9cd/likes.svg?style=flat)](https://juejin.im/entry/5998f8396fb9a0247c6ec9cd/detail)\r\n**Javascript是一门很吊的语言，我可能学了假的JavaScript，哈哈，大家还有什么推荐的，补充送那啥邀请码。**\r\n\r\n**本文秉承着：你看不懂是你SB，我写的代码就要牛逼。**\r\n\r\n### 1、单行写一个评级组件\r\n\"★★★★★☆☆☆☆☆\".slice(5 - rate, 10 - rate);定义一个变量rate是1到5的值，然后执行上面代码，看图\r\n\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipc4o8jujj30dx0fqjs7.jpg)\r\n\r\n才发现插件什么的都弱爆了\r\n\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipi6xsa7uj306o06odg0.jpg)\r\n来源：[来自知乎用户蜗牛老湿的回答](https://www.zhihu.com/question/46943112/answer/113583615)\r\n\r\n### 2、如何装逼用代码骂别人SB\r\n\r\n```JavaScript\r\n(!(~+[])+{})[--[~+\"\"][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]\r\n```\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fiph82ae7xj30f803gaab.jpg)\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipim72hyxj306j03pglk.jpg)\r\n了解为什么请移步：[一行能装逼的JavaScript代码](http://www.jfh.com/jfperiodical/article/3224)\r\n\r\n### 3、如何用代码优雅的证明自己NB\r\n这个牛逼了\r\n\r\n```JavaScript\r\n\tconsole.log(([][[]]+[])[+!![]]+([]+{})[!+[]+!![]])\r\n```\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipdkgl7myj30ch045aaa.jpg)\r\n\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipiacsfkfj30bo0agjrg.jpg)\r\n\r\n### 4、JavaScript 错误处理的方式的正确姿势\r\n😂😂😂，舅服你\r\n\r\n```JavaScript\r\ntry {\r\n    something\r\n} catch (e) {\r\n    window.location.href =\r\n        \"http://stackoverflow.com/search?q=[js]+\" +\r\n        e.message;\r\n}\r\n```\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipc7jv37sj30c809lq36.jpg)\r\n\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipic015pij307d05at8m.jpg)\r\n\r\n### 5、从一行代码里面学点JavaScript\r\n\r\n```JavaScript\r\n[].forEach.call($$(\"*\"),function(a){\r\n    a.style.outline=\"1px solid #\"+(~~(Math.random()*(1<<24))).toString(16)\r\n})\r\n```\r\n翻译成正常语言就是这样的\r\n\r\n```JavaScript\r\nArray.prototype.forEach.call(document.querySelectorAll('*'), \r\ndom => dom.style.outline = `1px solid #${parseInt(Math.random() * \r\nMath.pow(2,24)).toString(16)}`)\r\n```\r\n\r\n接下来在浏览器控制看看：something magic happens\r\n\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipgsovd4dj30xa0eln0f.jpg)\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipip3xx44j30go097mxf.jpg)\r\n具体分析请参见这篇文章：[从一行代码里面学点JavaScript](https://sdk.cn/news/3025)\r\n\r\n### 6、论如何优雅的取随机字符串\r\n\r\n```JavaScript\r\nMath.random().toString(16).substring(2) // 13位\r\nMath.random().toString(36).substring(2) // 11位\r\n```\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipckf384pj309803tt91.jpg)\r\n\r\n### 7、\\(10\\)\\[\"toString\"\\]\\(\\) === \"10\"\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fiphohmxl9j309f0570sx.jpg)\r\n解析请移步：[js奇淫技巧1](http://qylanikin.lofter.com/post/1cbb3f55_cc787c9)\r\n\r\n### 8、匿名函数自执行\r\n这么多写法你选择哪一种？我选择死亡。\r\n\r\n```JavaScript\r\n( function() {}() );\r\n( function() {} )();\r\n[ function() {}() ];\r\n\r\n~ function() {}();\r\n! function() {}();\r\n+ function() {}();\r\n- function() {}();\r\n\r\ndelete function() {}();\r\ntypeof function() {}();\r\nvoid function() {}();\r\nnew function() {}();\r\nnew function() {};\r\n\r\nvar f = function() {}();\r\n\r\n1, function() {}();\r\n1 ^ function() {}();\r\n1 > function() {}();\r\n// ...\r\n```\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipirtvdxnj30iu0g80tl.jpg)\r\n\r\n### 9、另外一种undefined\r\n从来不需要声明一个变量的值是undefined，因为JavaScript会自动把一个未赋值的变量置为undefined。所有如果你在代码里这么写，会被鄙视的\r\n\r\n```JavaScript\r\nvar data = undefined;\r\n```\r\n但是如果你就是强迫症发作，一定要再声明一个暂时没有值的变量的时候赋上一个undefined。那你可以考虑这么做：\r\n\r\n```JavaScript\r\n var data = void 0; // undefined\r\n```\r\nvoid在JavaScript中是一个操作符，对传入的操作不执行并且返回undefined。void后面可以跟()来用，例如void(0)，看起来是不是很熟悉？没错，在HTML里阻止带href的默认点击操作时，都喜欢把href写成javascript:void(0)，实际上也是依靠void操作不执行的意思。\r\n\r\n当然，除了出于装逼的原因外，实际用途上不太赞成使用void，因为void的出现是为了兼容早起ECMAScript标准中没有undefined属性。void 0的写法让代码晦涩难懂。\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fiph76wloqj30dw0ai0ta.jpg)\r\n\r\n### 10、论如何优雅的取整\r\n\r\n```JavaScript\r\nvar a = ~~2.33\r\n\r\nvar b= 2.33 | 0\r\n\r\nvar c= 2.33 >> 0\r\n```\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipcqcollxj308z05g74e.jpg)\r\n\r\n### 11、如何优雅的实现金钱格式化：1234567890 --> 1,234,567,890\r\n用正则魔法实现：\r\n\r\n```JavaScript\r\nvar test1 = '1234567890'\r\nvar format = test1.replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',')\r\n\r\nconsole.log(format) // 1,234,567,890\r\n```\r\n非正则的优雅实现：\r\n\r\n```JavaScript\r\n function formatCash(str) {\r\n       return str.split('').reverse().reduce((prev, next, index) => {\r\n            return ((index % 3) ? next : (next + ',')) + prev\r\n       })\r\n}\r\nconsole.log(formatCash('1234567890')) // 1,234,567,890\r\n```\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipivjpb63j306o05lmzr.jpg)\r\n\r\n###  12、这个我服，还有这个你很机智\r\n我服\r\n\r\n```JavaScript\r\nwhile (1) {\r\n    alert('牛逼你把我关了啊')\r\n}\r\n```\r\n你很机智，好一个障眼法\r\n\r\n```html\r\n清除缓存： <a href=\"javascript:alert('清除成功');\">清除缓存</a>\r\n```\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipiexe82vj307o053746.jpg)\r\n\r\n### 13、逗号运算符\r\n\r\n```JavaScript\r\nvar a = 0; \r\nvar b = ( a++, 99 ); \r\nconsole.log(a);  // 1\r\nconsole.log(b);  // 99\r\n```\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipetbjrkwj30d204v0t1.jpg)\r\n\r\n### 14、论如何最佳的让两个整数交换数值\r\n常规办法：\r\n\r\n```JavaScript\r\nvar a=1,b=2;\r\na += b;\r\nb = a - b;\r\na -= b;\r\n```\r\n缺点也很明显，整型数据溢出，对于32位字符最大表示数字是2147483647，如果是2147483645和2147483646交换就失败了。\r\n黑科技办法：\r\n\r\n```JavaScript\r\na ^= b;\r\nb ^= a;\r\na ^= b;\r\n```\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipd3k9c8zj309706cwen.jpg)\r\n\r\n哈哈😄，看不懂的童鞋建议去补习一下C语言的位操作，我就不去复习了，以前学嵌入式时候学的位操作都忘了\r\n\r\n### 15、实现标准JSON的深拷贝\r\n\r\n```JavaScript\r\nvar a = {\r\n    a: 1,\r\n    b: { c: 1, d: 2 }\r\n}\r\nvar b=JSON.parse(JSON.stringify(a))\r\n```\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipfjzx1w9j30bc06dt93.jpg)\r\n\r\n不考虑IE的情况下，标准JSON格式的对象蛮实用，不过对于undefined和function的会忽略掉。\r\n\r\n### 16、不用Number、parseInt和parseFloat和方法把\"1\"字符串转换成数字\r\n哈哈，不准用强制类型转换，那么就想到了强大了隐式转换\r\n\r\n```JavaScript\r\nvar a =1 \r\n+a\r\n```\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipdb6yztnj307y04h3yj.jpg)\r\n\r\n### 17、如何装逼的写出\"hello world!\"\r\n\r\n滚动条很长哦😯\r\n\r\n```JavaScript\r\n([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[+[]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[+[]]+([][[]]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()([][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]+!![]+!![]+!![]+!![]+[]))+([]+{})[+!![]]+(!![]+[])[+!![]]+(![]+[])[!+[]+!![]]+([][[]]+[])[!+[]+!![]]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[+[]]+([][[]]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()([][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(![]+[])[!+[]+!![]+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+([]+[][(![]+[])[!+[]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]]+(![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+[]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+([][[]]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!![]]+([][[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[!+[]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]]+(+{}+[])[+!![]]+(!![]+[])[+[]]+([][[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][[]]+[])[+!![]])())[!+[]+!![]+!![]]+([][[]]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+(!+[]+!![]+[])+(+!![]+[]))\r\n```\r\n居然能运行，牛逼的隐式转换\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipe2e4w5wj30hs08ota4.jpg)\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipixk4m50j309z078mxt.jpg)\r\n\r\n### 18、parseInt(0.0000008) === 8\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipbwrvnq0j307i03pmx8.jpg)\r\n\r\n![](https://ws4.sinaimg.cn/large/006tKfTcgy1fipigonkitj307e074t92.jpg)\r\n\r\n### 19、++[[]][+[]]+[+[]] == 10\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipbyod0sxj308y04smx8.jpg)\r\n\r\n强大的隐式转换，23333\r\n\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipihgatwvj3069069jra.jpg)\r\n\r\n### 20、0.1 + 0.2 == 0.3\r\n\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipc0vvbnsj306l03rglm.jpg)\r\n\r\n`0.1 +0.2 == 0.3` 竟然是不成立的。。。。所以这就是为什么数据库存储对于货币的最小单位都是分。\r\n\r\n简单说，`0.1`和`0.2`的二进制浮点表示都不是精确的，所以相加后不是`0.3`，接近（不等于）\r\n`0.30000000000000004`。\r\n\r\n所以，比较数字时，应该有个宽容值。ES6中这个宽容值被预定义了：`Number.EPSILON`。\r\n\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipiiub4lqj3068061a9x.jpg)\r\n\r\n### 21、最短的代码实现数组去重\r\n\r\n```JavaScript\r\n[...new Set([1, \"1\", 2, 1, 1, 3])]\r\n```\r\n前不久面试阿里就问了这道题，哈哈，所以也写上一下\r\n\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipecc1ftkj30a603tglr.jpg)\r\n\r\n### 22、用最短的代码实现一个长度为m(6)且值都n(8)的数组\r\n\r\n```JavaScript\r\nArray(6).fill(8)\r\n```\r\n这个够短了吧，好像是当初哪里看到的一个面试题，就自己想到了ES6的一些API\r\n\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipehmuow7j30a604ft8u.jpg)\r\n\r\n\r\n### 23、短路表达式\r\n条件判断\r\n\r\n```JavaScript\r\nvar a = b && 1\r\n    // 相当于\r\nif (b) {\r\n    a = 1\r\n} else {\r\n    a = b\r\n}\r\n\r\nvar a = b || 1\r\n    // 相当于\r\nif (b) {\r\n    a = b\r\n} else {\r\n    a = 1\r\n}\r\n```\r\n#### 24、JavaScript版迷宫\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipe4dm5zuj30go08ogmd.jpg)\r\n逃出迷宫，2333\r\n![](https://ws2.sinaimg.cn/large/006tKfTcgy1fipizki4i1g30c80c874n.gif)\r\n\r\n### 25、取出一个数组中的最大值和最小值\r\n\r\n```JavaScript\r\nvar numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; \r\nvar maxInNumbers = Math.max.apply(Math, numbers); \r\nvar minInNumbers = Math.min.apply(Math, numbers);\r\n```\r\n![](https://ws3.sinaimg.cn/large/006tKfTcgy1fipelb9ry7j30dt062q3j.jpg)\r\n\r\n### 26、将argruments对象转换成数组\r\n\r\n```JavaScript\r\nvar argArray = Array.prototype.slice.call(arguments);\r\n\r\n或者ES6：\r\n\r\nvar argArray = Array.from(arguments)\r\n```\r\n\r\n### 27、javascript高逼格之Function构造函数\r\n很多JavaScript教程都告诉我们，不要直接用内置对象的构造函数来创建基本变量，例如var arr = new Array(2); 的写法就应该用var arr = [1, 2];的写法来取代。\r\n\r\n但是，Function构造函数（注意是大写的Function）有点特别。Function构造函数接受的参数中，第一个是要传入的参数名，第二个是函数内的代码（用字符串来表示）。\r\n\r\n```JavaScript\r\nvar f = new Function('a', 'alert(a)');\r\nf('jawil'); // 将会弹出窗口显示jawil\r\n```\r\n这种方式可以根据传入字符串内容来创建一个函数 是不是高大上？！\r\n![](https://ws1.sinaimg.cn/large/006tKfTcgy1fipj2ajcxmj3051054q2y.jpg)\r\n\r\n### 28、从一个数组中找到一个数，O(n)的算法，找不到就返回 null。\r\n正常的版本:\r\n\r\n```JavaScript\r\nfunction find (x, y) {\r\n  for ( let i = 0; i < x.length; i++ ) {\r\n    if ( x[i] == y ) return i;\r\n  }\r\n  return null;\r\n}\r\n \r\nlet arr = [0,1,2,3,4,5]\r\nconsole.log(find(arr, 2))\r\nconsole.log(find(arr, 8))\r\n```\r\n结果到了函数式成了下面这个样子（好像上面的那些代码在下面若影若现，不过又有点不太一样，为了消掉if语言，让其看上去更像一个表达式，动用了 ? 号表达式）：\r\n\r\n```JavaScript\r\n//函数式的版本\r\nconst find = ( f => f(f) ) ( f =>\r\n  (next => (x, y, i = 0) =>\r\n    ( i >= x.length) ?  null :\r\n      ( x[i] == y ) ? i :\r\n        next(x, y, i+1))((...args) =>\r\n          (f(f))(...args)))\r\n \r\nlet arr = [0,1,2,3,4,5]\r\nconsole.log(find(arr, 2))\r\nconsole.log(find(arr, 8))\r\n```\r\n[如何读懂并写出装逼的函数式代码](http://coolshell.cn/articles/17524.html)\r\n\r\n**最后奉劝大家一句：莫装逼、白了少年头，2333。。。原文收录在我的 GitHub博客 (https://github.com/jawil/blog) ，喜欢的可以关注最新动态，大家一起多交流学习，共同进步，以学习者的身份写博客，记录点滴。**\r\n"},"comment":{"url":"https://api.github.com/repos/jawil/blog/issues/comments/323911643","html_url":"https://github.com/jawil/blog/issues/24#issuecomment-323911643","issue_url":"https://api.github.com/repos/jawil/blog/issues/24","id":323911643,"user":{"login":"mqyqingfeng","id":11458263,"avatar_url":"https://avatars3.githubusercontent.com/u/11458263?v=4","gravatar_id":"","url":"https://api.github.com/users/mqyqingfeng","html_url":"https://github.com/mqyqingfeng","followers_url":"https://api.github.com/users/mqyqingfeng/followers","following_url":"https://api.github.com/users/mqyqingfeng/following{/other_user}","gists_url":"https://api.github.com/users/mqyqingfeng/gists{/gist_id}","starred_url":"https://api.github.com/users/mqyqingfeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mqyqingfeng/subscriptions","organizations_url":"https://api.github.com/users/mqyqingfeng/orgs","repos_url":"https://api.github.com/users/mqyqingfeng/repos","events_url":"https://api.github.com/users/mqyqingfeng/events{/privacy}","received_events_url":"https://api.github.com/users/mqyqingfeng/received_events","type":"User","site_admin":false},"created_at":"2017-08-22T03:49:09Z","updated_at":"2017-08-22T03:49:09Z","body":"我来补充一个\"i love you\":\r\n\r\n```js\r\n[[][0] + []][0][5]+[[][[[][0] + []][0][4]+[[][0] + []][0][5]+[[][0] + []][0][1]+[[][0] + []][0][2]] + []][0][8]+[[[] == []][0] + []][0][2]+[[][[[][0] + []][0][4]+[[][0] + []][0][5]+[[][0] + []][0][1]+[[][0] + []][0][2]] + []][0][6]+[[][[[][0] + []][0][4]+[[][0] + []][0][5]+[[][0] + []][0][1]+[[][0] + []][0][2]]+[]][0][23]+[[][0] + []][0][3]+[[][[[][0] + []][0][4]+[[][0] + []][0][5]+[[][0] + []][0][1]+[[][0] + []][0][2]] + []][0][8]+[+[1 + [[][0] + []][0][3] +309][0] + []][0][7]+[[][[[][0] + []][0][4]+[[][0] + []][0][5]+[[][0] + []][0][1]+[[][0] + []][0][2]] + []][0][6]+[[][0] + []][0][0]\r\n```"}}
{"actor":{"display_login":"mqyqingfeng"}}