1.Javascript运算符(1):逻辑非!;相等运算符==(Ⅰ)

来源:互联网 发布:域名注册com 编辑:程序博客网 时间:2024/05/17 03:11

(1)<script type="text/javascript">  function check(obj){  var uname=obj.value;  if(uname==""){  alert("用户名不能为空");  }}  </script><input type="text" onblur="check(this);"/>(2)<script type="text/javascript"> function check(obj){var uname=obj.value;  if(!uname){  alert("用户名不能为空");  }}  </script><input type="text" onblur="check(this);"/>

(1)和(2)效果是一样的

(1)中用==比较 ==比较的是什么呢 <JavaScript: The Definitive Guide, 5th Edition> 5.4.1的相关描述:

In JavaScript, numbers, strings, and boolean values are compared by value. In this case, two separate values are involved, and the == and === operators check that these two values are identical. This means that two variables are equal or identical only if they contain the same value. For example, two strings are equal only if they each contain exactly the same characters.

JavaScript中比较数字,字符串,布尔值时使用的是值(value),当且仅当这两个变量存放的值完全等同时它们才相等(equal )或等同(identical )

在这里用===(identity operator)和==(equality operator)效果是一样的 因为uname和""数据类型相同  区别是==允许类型转换

(1)中的 uname=="" 直接判断值是否相等 结果为true或false

(2)中使用逻辑非!来判断  <JavaScript..>关于!的描述

5.7.3. Logical NOT (!)The ! operator is a unary operator; it is placed before a single operand. Its purpose is to invert the boolean value of its operand. For example, if the variable a has the value true (or is a value that converts to true), !a has the value false. And if the expression p && q evaluates to false (or to a value that converts to false), !(p && q) evaluates to true.The ! operator converts its operand to a boolean value (using the rules described in Chapter 3)if necessary before inverting the converted value. This means that you can convert any value x to its equivalent boolean value by applying this operator twice: !!x.
运算!uname时,!先将它的运算数uname转换为一个布尔类型的值(string-->boolean)
如果uname为空("") 它被转换为false 否则转换为true

附:==运算符的运算规则
The following rules determine whether two values are equal according to the == operator:If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality:If one value is null and the other is undefined, they are equal.If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.If either value is TRue, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again.If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.Any other combinations of values are not equal.

原创粉丝点击