电台复活节

by Ethan Ryan

由伊桑·瑞安(Ethan Ryan)

如何通过在控制台中隐藏复活节彩蛋使您的应用程序用户惊讶 (How to surprise your app’s users by hiding Easter eggs in the console)

I love console.logging(“stuff”).

我喜欢console.logging(“ stuff”)。

I do it throughout my apps, for debugging purposes and feature building purposes, and just for the sheer hell of it. It’s fun to log stuff to the console.

我在整个应用程序中都这样做,出于调试目的和功能构建目的,并且纯粹出于此目的。 将内容记录到控制台很有趣。

I even use console.warn() and console.error(), and console.table() if I’m feeling frisky.

如果我感到烦躁,甚至可以使用console.warn()console.error()以及console.table()

I like all the pretty colors they make in my console, and sometimes you want some messages to stand out more than others.

我喜欢它们在控制台中制作的所有漂亮颜色,有时您希望某些消息比其他消息更加突出。

But I realized while looking at my story generator app WordNerds yesterday than I was logging to the console in production mode.

但是我昨天在看故事生成器应用程序WordNerds时发现 ,与在生产模式下登录控制台相比,我意识到了这一点。

Uh-oh spaghetti-ohs.

哦,意大利面条,哦。

That’s a no-no. It could slow down the code unnecessarily, and more importantly, it could compromise my users’ email addresses! I was logging all my users’ usernames and email addresses. Not cool! Their passwords are encrypted of course but still, no bueno. I wouldn’t want any bad guys getting a a bunch of my users’ email addresses and spamming them crapola.

那是不对的。 它可能会不必要地降低代码速度,更重要的是,它可能会损害我的用户的电子邮件地址! 我正在记录所有用户的用户名和电子邮件地址。 不酷! 他们的密码当然是加密的,但仍然没有加密。 我不希望任何坏蛋得到我用户的电子邮件地址,并给他们发送垃圾邮件。

在生产模式下摆脱控制台日志 (Getting Rid of Console Logs In Production Mode)

Fixing it turned out to be easy. Sure, I could have gone through the codebase and commented out all my console.logs(), but that would be a pain, and some of them are serving important purposes in development mode.

修复它很容易。 当然,我可以遍历代码库并注释掉我的所有console.logs(),但这很痛苦,其中一些在开发模式下起着重要的作用。

Luckily there’s an easier, better way.

幸运的是,有一种更简便,更好的方法。

First I consulted some of the solutions to this problem listed on StackFlow, and then ultimately went with the first solution listed on this blog post.

首先,我咨询了 StackFlow上针对此问题的一些解决方案 ,然后最终采用了此博客文章中列出的第一个解决方案。

As some of the comments mentioned when someone listed this as a solution to the problem: “That’s a hack. Your [sic] wasting computation in production”

正如有人将其列为问题解决方案时提到的一些评论:“这是黑客。 您的[原文如此]浪费了生产中的计算量”

Good debate! I wasn’t too worried about calling an empty function several times and wasting some computation in production, so I went with this solution, because it’s easy to implement and solves my problem.

好辩论! 我不太担心多次调用空函数并浪费生产中的计算量,因此我选择了此解决方案,因为它易于实现并解决了我的问题。

Here’s how I did it, in the src/index.js file:

这是我在src / index.js文件中执行的操作:

Of course I could do this in any file, like the App component, or my StoryContainer component. Anywhere as long as it was before any console logs or warns or errors were being rendered. But it made sense to me to do it at the root.

当然,我可以在任何文件中执行此操作,例如App组件或StoryContainer组件。 只要在呈现任何控制台日志或警告或错误之前的任何时间。 但是从根本上讲,这对我来说很有意义。

I tested it in development by replacing ‘production’ with ‘development’, and it worked! No more messages in the console.

我在开发中通过将“生产”替换为“开发”来对其进行了测试,并且成功了! 控制台中没有更多消息。

将消息添加回控制台 (Adding Messages Back Into the Console)

But then I felt sad :(

但是后来我很难过:(

No more messages in the console? Seemed so sparse.

控制台中没有更多消息了吗? 看起来如此稀疏。

May as well have SOME messages for those curious, intrepid word nerds daring enough to open up the console.

对于那些大胆地打开控制台的好奇,勇敢的书呆子,可能还会收到一些消息。

So I added one back in, like a hidden Easter egg:

所以我又添加了一个,就像一个隐藏的复活节彩蛋 :

How’d I do this? Easy: since all my app’s calls to console.log(), console.warn(), and console.error() where being overwritten by empty functions, I simply added in a console.info()! It’s basically the same as a console.log(). Some of the differences are listed, and disputed, here.

我该怎么做? 容易:因为我的应用程序对console.log()console.warn()console.error()所有调用都被空函数覆盖,所以我只添加了console.info() ! 它基本上与console.log()相同。 这里列出了一些差异,并且有争议。

hello everybody! was a little boring though. I already had my app’s logged-in user’s name stored in state, so why not personalize my message?

hello everybody! 虽然有点无聊。 我已经将应用程序的登录用户名存储在状态中,那么为什么不个性化我的消息呢?

And if I’m gonna personalize my message, why not personalized a bunch of messages, and randomly return one every time a logged-in user inspects the console? Everyone likes finding Easter eggs!

而且,如果我要个性化我的消息,为什么不个性化一堆消息,并在每次登录用户检查控制台时随机返回一条消息? 每个人都喜欢找到复活节彩蛋!

That’s what I decided to do, and here’s how I did it:

那就是我决定要做的,这就是我的做法:

I’m rendering my Greeting component in my StoryContainer, so that whenever a logged-in user chooses to check out the console, they’ll see one of those friendly messages!

我将在StoryContainer中渲染我的Greeting组件,以便每当登录用户选择检出控制台时,他们将看到这些友好消息之一!

function getFriendlyMessage(nameString) {let messages = [`Hello ${nameString}, it's good to see you!`,`sup ${nameString}`,`hi there ${nameString}, you look awesome today!`,`hi there ${nameString}, you spectacular human being you!`,`you look awesome today ${nameString}!`,`hellllooooooo ${nameString}!`,`Hey ${nameString}, how's life?`,`Can you keep a secret, ${nameString}? You're my favorite!`,`Nothing to see here, ${nameString}.`,`Congratulations, ${nameString}! You've discovered the console ;)`,`have i told you lately that i love you, ${nameString}?`,`i knew you'd find this Easter egg eventually, ${nameString}...`,]var randomMessage = messages[Math.floor(Math.random() * messages.length)];return randomMessage
}

Coding is fun.

编码很有趣。

Thanks for reading, word nerds!

感谢您的阅读,书呆子!

翻译自: https://www.freecodecamp.org/news/how-to-surprise-your-apps-users-by-hiding-easter-eggs-in-the-console-3b6e9285e7e7/

电台复活节

电台复活节_如何通过在控制台中隐藏复活节彩蛋使您的应用程序用户惊讶相关推荐

  1. 海贼王为什么画风突变_突变对象时控制台中会记录什么

    海贼王为什么画风突变 by Boris Sever 通过鲍里斯·塞弗(Boris Sever) 突变对象时控制台中会记录什么 (What gets logged in the console when ...

  2. python打开控制台运行_如何在IPython控制台中默认运行文件而不是终端?

    我在PyCharm开始了一个新项目.我安装了Anaconda 3.6.所以,在PyCharm中,我选择了Anaconda python.exe作为项目解释器. 当我第一次运行PyCharm时,它使用I ...

  3. php结尾的链接_优化 PHP 代码建议(结尾有彩蛋)

    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id'] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用ech ...

  4. datagrid底部显示水平滚动_看完《惊奇队长》等彩蛋,我想到了一个制作PPT滚动字幕的方法...

    滚动字幕大家都不陌生,每每看完电影,影院亮灯的时候就能看到: 电影片尾滚动字幕 前两天去看了<惊奇队长>,在等彩蛋的过程中,看着一行行的字幕在面前滚动,我突然想到:这种滚动字幕式动画,其实 ...

  5. pc模拟微信二维码背景_[深入浅出Windows 10]模拟实现微信的彩蛋动画

    9.7 模拟实现微信的彩蛋动画 大家在玩微信的时候有没有发现节日的时候发一些节日问候语句如"情人节快乐",这时候会出现很多爱心形状从屏幕上面飘落下来,我们这小节就是要模拟实现这样的 ...

  6. 不可思议有氧机器人_不思议迷宫这七个彩蛋机器人你都有了嘛

    一.桑尼机器人 未来猫出战可以触发操作台钻石彩蛋,和圣坛操作台共享上限. 大盗神概率触发桑尼,未来猫修复后入手桑尼机器人. 桑尼机器人:战力+6000,幸运+2. 二.蜘蛛机器人 魔法称号走位面,用魔 ...

  7. android7.1.2彩蛋,在Android 7.0牛轧糖中解锁秘密猫收集复活节彩蛋 | MOS86

    我们已经向您展示了Android 5.0棒棒糖和6.0棉花糖中的Flappy Bird复活节彩蛋,现在我们为Android 7.0牛轧糖提供了一个新彩蛋. 如果您是Android用户,并且您的设备运行 ...

  8. html5 彩蛋动画,egg.js-趣味复活节彩蛋js插件

    Egg.js是一款非常有趣的复活节彩蛋js插件.该复活节彩蛋插件可以监控用户的键盘输入,当用户输入正确的字母序列的时候,复活节彩蛋会打开,在页面中显示你意想不到的内容. 十二世纪时,人们在复活节节庆中 ...

  9. 你如何在java中获取线程堆_如何在Windows上获取未在控制台中运行的Java进程的线程和堆转储...

    问题 我有一个Java应用程序,我从控制台运行,然后控制台执行另一个Java进程.我想获得该子进程的线程/堆转储. 在Unix上,我可以做akill -3 但是在Windows AFAIK上获取线程转 ...

  10. C#_解决在控制台中输入Ctrl+Z的问题

    本人在前几天做了一道题如下(在116行中用(int)cki.KeyChar==26解决了C#中在控制台捕捉Ctrl+Z): 解决的方法也是请教了老师,经老师调试过才得出的解决方法.(因在Console ...

最新文章

  1. mysql or的效率_Mysql比较exists与in以及or的效率分析
  2. python提供两个对象身份比较操作符什么和什么来测试_python - 第二部分
  3. 零基础可以学python吗-初学者必知:零基础学习Python真的能学会吗?
  4. mysql query profile_MySQL Query Profile
  5. js vue中得延时器_Vue.js实现时分秒倒计时
  6. 遇见未来 | 超融合如何兼顾企业的“敏态”和“稳态”的业务需求
  7. CCNP实验---EIGRP自动汇总
  8. Web前端开发工具和框架
  9. 通过doi可以检索到文献_什么是DOI?如何获取文献的DOI?
  10. kali-Linux局域网渗透之Win2008
  11. FTP无法在资源管理器中打开
  12. 求集合中最大值、最小值、和
  13. 榆树计算机课,吉林省榆树市弓棚镇武龙中学校七年级信息技术:第九课 Excel表格计算 教案+课件 (2份打包)...
  14. [面试笔试整理1]:深度学习机器学习面试问题准备(必会)
  15. KL-MPLC无人值守系统
  16. 怎么可以修改pr基本图形中的文字_10、Pr中基本图形安装使用,点点就可以应用高级的字幕...
  17. Android Navigation Bar
  18. PostgreSQL修炼之道之SQL语言入门(四)
  19. HackPwn:TCL智能洗衣机破解细节分析
  20. 2024在职考研|MBA/MPA/MEM管理类硕士报考流程及基础问题扫盲

热门文章

  1. AI公开课:03月26日未来十年 AI如何进化—圆桌探讨(乌镇智库理事长、CSDN 创始人董事长、智源人工智能研究院副院长)之《AI:昨天 · 今天 · 明天》
  2. matlab求fft频谱峰值程序,用FFT对信号作频谱分析Matlab程序
  3. 从亏损19亿到盈利6亿,恺英网络做对了什么?
  4. Java设计模式 -- GOF23
  5. 渗透测试-Kali Linux 正确清理垃圾的姿势
  6. 用c/c++和Java语言实现庞加莱回归
  7. python怎么交换xy轴_matplotlib Y轴和X轴交换
  8. spring学习笔记 (6)使用mybatis操作数据库增删改查
  9. Windows记事本编码反汇编分析
  10. that being said