使用“return!0”是否有任何意义?在JavaScript中? [英] Is there any point of using "return !0" in javascript?

查看:140
本文介绍了使用“return!0”是否有任何意义?在JavaScript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您转到Google结果页并运行 rwt.toString(),您会看到此函数的返回调用是:

If you go to a google result page, and run rwt.toString(), you'll see that the return call for this function is:

return !0;

我想不出为什么这不会总是。这只是 true 的简写,还是有更多的进展?

I can't think of any reason why this wouldn't always be true. Is this just a shorthand for true, or is there more going on here?

推荐答案

总是如此,但需要2个字节才能下载(!0 是2个字符)而不是4个字节来下载布尔值 true

It is always true, but it takes 2 bytes to download (!0 is 2 characters) instead of 4 bytes to download the boolean value true.

大多数Javascript minifiers将 true 转换为!0 和false !1 。您可以通过在Google的Closure Compiler上使用简单优化键入 var y = true; 来查看此示例: http://closure-compiler.appspot.com/home

Most Javascript minifiers will convert true to !0 and false to !1. You can see an example of this by typing var y = true; with Simple optimizations on Google's Closure Compiler: http://closure-compiler.appspot.com/home

这篇关于使用“return!0”是否有任何意义?在JavaScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆