admin管理员组文章数量:1316393
Why can't I do this:
var fooElement, barElements; if(fooElement = document.getElementById('foo') && barElements = fooElement.getElementsByTagName('bar') && barElements[0] && barElements[0].onclick) { console.log(barElements[0].onclick); }
This won't work either:
var foo, bar; if(foo = true && bar = true) { console.log('yay'); }
Why can't I do this:
var fooElement, barElements; if(fooElement = document.getElementById('foo') && barElements = fooElement.getElementsByTagName('bar') && barElements[0] && barElements[0].onclick) { console.log(barElements[0].onclick); }
This won't work either:
var foo, bar; if(foo = true && bar = true) { console.log('yay'); }Share Improve this question asked Aug 21, 2011 at 17:14 TyiloTyilo 30.2k41 gold badges122 silver badges201 bronze badges
2 Answers
Reset to default 10Try this (it should give you a clue):
var foo, bar;
if((foo = true) && (bar = true))
{
console.log('yay');
}
Check Operator Precedence,Use
if((foo = true) && (bar = true))
{
alert(foo);
}
UPD: Don't forget that following code will not set bar
to true because && is Short Circuit
operator
if((foo = false) && (bar = true))
{
alert(foo);
}
Sample
本文标签: javascriptMultiple assignments inside ifstatementStack Overflow
版权声明:本文标题:javascript - Multiple assignments inside if-statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741997996a2410437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论