日期:2014-05-16  浏览次数:20538 次

!&&3=3,1||3=1为什么?
如题,谢谢!!!!!!!

------解决方案--------------------
&&

This is the logical AND operator, which returns a Boolean true if both the operands are true. Logically it follows that if the first operand is false, then the whole expression is false, and this is how the operator works; It first evaluates the left hand operand, and if this returns false then, without going any further, it returns false for the whole expression. Otherwise it returns the value of the second operand: true or false for a Boolean value, or the actual value itself if non-Boolean. Assuming 'a ' to be 3, 'b ' to be 5, and 'c ' to be 3, the following examples all return true:

------解决方案--------------------
手册上这么说的
&&
如果&&运算符左边的值可以转换成false (这些-- null,0,undefined, " "--之一),那么&&就返回左边表达式的值,否则将继续计算第二个运算数,并且返回这个表达式的值
||
与&&雷同,只要||左边的值可以转换成true,就直接返回左边表达式的值,否则将继续计算第二个运算数,并且返回这个表达式的值

你得这个1&&3=3,1||3=1也就好解释了
1&&3中, 1可以转换成true,于是继续计算右边的3,3也可以转换成true,最后就返回3(表达式的值)了
1||3中,1为true,不用继续计算,直接返回1(也是表达式的值)