滥用断言_滥用console.log删除调试代码

滥用断言

One of the big efforts of this blog is to make it as fast and compact as possible. I shave bytes and do everything I can to make the site as lightning fast as possible. In looking at my site's main JavaScript file, I saw a few blocks which have no value on production, even after minification. After some basic experimentation, I realized that we can abuse console.log statements, which are removed by minifiers, to execute functions on development servers but not on production!

该博客的最大努力之一就是使其尽可能快和紧凑。 我会剃除字节并尽一切可能使该站点尽可能快。 在查看网站的主要JavaScript文件时,我看到了一些即使是缩小后也没有价值的生产块。 经过一些基本的实验,我意识到我们可以滥用由minifiers删除的console.log语句,以便在开发服务器上执行功能,而不能在生产服务器上执行功能!

JavaScript (The JavaScript)

The traditional call to console.log is one or several strings, but you can pass a self-executing function if you want:

传统上对console.log调用是一个或几个字符串,但是如果需要,您可以传递一个自执行函数:


console.log((function() {
  // Do whatever...

  // Example for local dev: convert live links to local

  // Return a string to be logged, if you'd like
  return "Debug: {x} has been executed and is now working";
})());


The console.log method really doesn't do much here, but we get the added benefit of not only function execution but removal during uglify runs.

console.log方法在这里实际上并没有做太多事情,但是我们不仅获得了函数执行的好处,而且还获得了在uglify运行期间删除的好处。

Using console.x methods is a big help during development, and it's awesome that we can bastardize a minifier to work during both development and productions!

在开发过程中使用console.x方法有很大的帮助,这真是太棒了,我们可以混蛋一个简化程序,以便在开发和生产过程中都能正常工作!

翻译自: https://davidwalsh.name/console-log-debug

滥用断言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值