true==false以某种方式计算为true
true == false evaluates to true somehow?
我一直在努力抓取一些使用OWASP CRSFGuard项目进行保护的网页。图书馆似乎导致我的一个请求获得401,所以我开始深入研究他们的代码,并注意到以下内容;
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if(current == target || current == 'localhost') {
result = true;
} else if(true == false) {
if(target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
据我所知,一定有执行此代码的实例;CCD_ 1。假设true == false
本质上是false,那么代码将如何到达该语句?这是JS的古怪之处吗(我知道我们没有使用严格的===等式,但说真的…)?
答案:它永远不会到达那个代码块。
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if (current == target || current == 'localhost') {
result = true;
} else if (true == false) {
if (target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
var trueFalse = document.getElementById('trueFalse');
trueFalse.innerHTML = isValidDomain('true', 'false') ? 'WTF!' : 'All is good in the JS World';
trueFalse.addEventListener('click', function(e) {
trueFalse.innerHTML = (true == false) ? 'WTF!' : 'All is good in the JS World Still';
});
<div id="trueFalse"></div>
我认为Blazemonger很可能是正确的。
else if
可能在某个时候有其他条件,无论出于什么原因,他们决定不想再执行该代码块,所以他们将条件更改为始终为false的条件。
程序员使用1 === 0
作为false
的指示也并不罕见。他们为什么要这么做,谁也猜不透。
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 根据元素和容器大小计算边距
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- AngularJS-使用'true'属性
- 使用D3.js计算带有字母间距的文本长度
- 使用CSS或JavaScript计算分页符的数量
- if(foo!==null)的计算结果为true,即使foo为null
- ngIf的计算结果始终为true
- 设置为负数的变量计算结果为true
- true==false以某种方式计算为true
- 为什么 {} == false 的计算结果为 false,而 [] == false 的计算结果为 true
- 为什么空的 JavaScript 数组在条件结构中的计算结果为 true
- 如何使用 CheckBox=“true” 执行计算
- window.jQuery==true的计算结果为false,尽管window.jQuery=false计算结果为tru
- 用javascript计算for循环中条件为true的次数
- JavaScript IF语句计算TRUE错误-为什么
- Regex for date的计算结果为true,即使字符错误
- 在angularjs中计算ng-if true语句
- 为什么 “” < {} 的计算结果为 true
- 在Javascript中,为什么'-' === '-'的计算结果为true?并且是破折号 (-) js 字符串中的转