美文网首页
JS无形装逼,最为致命

JS无形装逼,最为致命

作者: coder_girl | 来源:发表于2019-11-08 15:53 被阅读0次

    作者 | megatron

    炼金 | juejin.im/post/5cc55eb5e51d456e577f93f0

    单行简洁的代码很难维护(有时甚至难以理解),但这并不能阻止广大攻城狮们脑洞,在编写简洁的代码后获得一定的满足感。

    以下我最近的一些收藏 javascript精简代码集合。它们都可以在你的开发控制台中运行,你可以从控制台中查看运行结果。同时,我希望你能在评论中分享一些自己的藏品!

    日历

    创建过去七天的数组,如果将代码中的减号换成加号,你将得到未来7天的数组集合

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. [...Array(7).keys()].map(days =>newDate(Date.now()-86400000* days));

    </pre>

    生成随机ID

    在原型设计时经常使用的创建ID功能。但是我在实际项目中看到有人使用它。其实这并不安全

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. Math.random().toString(36).substring(2);

    </pre>

    获取URL的查询参数

    这个获取URL的查询参数代码,是我见过最精简的 QAQ``?foo=bar&baz=bing => {foo: bar, baz: bing}

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. q={};location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);q;

    </pre>

    本地时间

    通过一堆HTML,您可以创建一个本地时间,其中包含您可以一口气读出的源代码,它每秒都会用当前时间更新页面

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. <bodyonload="setInterval(()=>document.body.innerHTML=new Date().toLocaleString().slice(10,19))"></body>

    </pre>

    数组混淆

    随机更改数组元素顺序,混淆数组

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. (arr)=> arr.slice().sort(()=>Math.random()-0.5)

    </pre>

    生成随机十六进制代码(生成随机颜色)

    使用JavaScript简洁代码生成随机十六进制代码

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. '#'+Math.floor(Math.random()*0xffffff).toString(16).padEnd(6,'0');

    </pre>

    一个面试题

    这是一个臭名昭著的面试题,让你写出他的运行结果,受不了~

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. for(i=0;++i<101;console.log(i%5?f||i:f+'Buzz'))f=i%3?'':'Fizz'

    </pre>

    数组去重

    这是一个原生的JS函数但是非常简洁,Set接受任何可迭代对象,如数组[1,2,3,3],并删除重复项

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. [...newSet(arr)]

    </pre>

    创建特定大小的数组

    方便快捷创建特定大小的数组

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. [...Array(3).keys()]

    </pre>

    返回一个键盘(惊呆了)

    这是一个很难看懂的简洁代码,但是运行后你会惊呆的,他竟然返回一个图形键盘

    <pre class="" style="margin: 0px 0px 15px; padding: 15px 5px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(246, 248, 250); font-size: 13px; line-height: 1.5; overflow: auto; border-radius: 3px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">

    1. (_=>[..."1234567890-=QWERTYUIOP[]~ASDFGHJKL;'ZXCVBNM,./~"].map(x=>(o+=/${b='_'.repeat(w=x<y?2:' 667699'[x=["BS","TAB","CAPS","ENTER"][p++]||'SHIFT',p])}\|,m+=y+(x+' ').slice(0,w)+y+y,n+=y+b+y+y,l+=' __'+b)[73]&&(k.push(l,m,n,o),l='',m=n=o=y),m=n=o=y='|',p=l=k=[])&&k.join``

    2. ``)()`

    </pre>

    这是它的打印结果:

    image

    相关文章

      网友评论

          本文标题:JS无形装逼,最为致命

          本文链接:https://www.haomeiwen.com/subject/vbidbctx.html