滥用断言
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
方法有很大的帮助,这真是太棒了,我们可以混蛋一个简化程序,以便在开发和生产过程中都能正常工作!
滥用断言