简体   繁体   English

0是什么? 在javascript中的意思

[英]What does 0; means in javascript

Looking at grunt-strip plugin to remove console.logs. 查看grunt-strip插件删除console.logs。 I realized that it replaces console.log statements with 0; 我意识到它用0;替换console.log语句0; . Does 0; 是否为0; have any effect? 有什么作用吗?

It doesn't have any effect, no. 它没有任何作用,不。 JS evaluates 0 and nothing picks up the result. JS评估为0 ,没有任何结果。 The idea is that removing console.* (with a simple replace by nothing) might break code like this: 这个想法是删除console.* (简单替换为空)可能会破坏如下代码:

if(condition)
    console.log('');

functionCall();

Would become (with reformatting for emphasis...) 将会成为(通过重新格式化来强调...)

if(condition)
    functionCall();

So it's replaced by a dummy statement. 因此,它由一个伪语句代替。

if(condition)
    0; // Does nothing

functionCall();

Also, code checking if the console is present will return false because 0 is falsy. 此外,因为0为假,所以检查控制台是否存在的代码将返回false。

if(console) { // Not executed when replaced by if(0)
    // debugging action
}

How it's done 怎么做

Right now, the logic consists of a simple replacement of your selected nodes with a falsy statement (0). 现在,逻辑包括用一个伪造的语句(0)简单替换您选择的节点。 This is proving to work in all reasonable situations and alleviates the need for complex rewrite logic. 事实证明,这适用于所有合理的情况,并减轻了对复杂重写逻辑的需求。

At some later date that rewrite logic may be important, but the cost vs reward isn't there right now. 在以后的某个日期,重写逻辑可能很重要,但是现在还不存在成本与报酬的关系。

Source: https://github.com/jsoverson/grunt-strip 资料来源: https : //github.com/jsoverson/grunt-strip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM