简体   繁体   English

你为什么要在javascript中使用0> 0?

[英]Why would you use 0 > 0 in javascript?

I was going through some of StackOverflow's client side code and I ran across this block of JavaScript in the source-code of https://stackoverflow.com/questions/ask : 我正在浏览一些StackOverflow的客户端代码,我在https://stackoverflow.com/questions/ask的源代码中遇到了这个JavaScript块:

if ($answerCheckbox.is(':checked') || 0 > 0) {
     $answerCheckbox.attr('checked', true);
     $('#question-only-section').hide();
     StackExchange.using("editor", function () {
          setTimeout(function () { showAnswerSection(true) }, 2);
     });
}

Why wouldn't you use false instead? 你为什么不用false而不是?

You're assuming the code is all natively written Javascript. 你假设代码都是本机编写的Javascript。 It isn't uncommon to see some server-generated script which references elements via some programmatic identifier which resolves like this at runtime, which admittedly looks a little peculiar. 看到一些服务器生成的脚本通过某些程序标识符引用元素并不罕见,这些标识符在运行时就像这样解析,这看起来有点奇怪。

它是生成的代码(不在.js文件中),所以显然这两个值中的一个并不总是0而是服务器端变量。

There is no reason for it... but until you know the server side code, you can't know for sure. 没有理由......但是在你知道服务器端代码之前,你无法确定。

Let's say (PHP) you had a variable $x=1 and could also be $x=0 depending on the scenario. 假设(PHP)你有一个变量$x=1 ,也可能是$x=0具体取决于场景。

if ($answerCheckbox.is(':checked') || <?php echo $x;?> > 0) {

That code makes perfect sense.... 那段代码很有道理......

Because that line probably comes from php, like so: 因为那行可能来自php,如下所示:

if ($answerCheckbox.is(':checked') || <?php echo $tot; ?> > 0) {

I know because in some situations I had to write code like that. 我知道因为在某些情况下我必须编写类似的代码。

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

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