[英]Undefined or null variable in function
在这些例子中,我正在破坏一个有条件的函数;
function validPrice(price)
{
if(price=="" || price==0 || price==null || price==undefined )
{
//do something
}
else
{
// do something different
}
}
var priceNew = $("li#listedProd").attr("price");
validPrice(priceNew);
我的问题是这些条件price=="" || price==0 || price==null || price==undefined
有什么不同? price=="" || price==0 || price==null || price==undefined
谁先写了那个代码就是谁
a)对未来使用非常防御。 b)不了解attr
工作原理。
方法attr
(或getAttribute
的基础调用将返回
null
,如果不是。 重要的是,如果有一个0
值,它将是一个字符串 0
,
因此不会在下面的测试中
捕获 - 捕获price == 0
测试,因为系统会自动将其转换为数字作为一部分==
比较。
if(price=="" || price==0 || price==null || price==undefined )
而且,由于转换在内部起作用的方式,所以这些测试无法按预期进行。 ==
和===
是不同的。 它应该是:
if(price === "" || price === 0 || price === null || price === undefined )
由于如何强制布尔运算,所有这些都可以轻松地简化为简单的“价格”:
if (!price)
或者,如果要捕获"0"
值
if (!price || +price === 0)
(+价格迫使价格成为一个数字,所以我们捕获0
0.00
和其他变化。)
让我们逐个看看你的条件陈述:
price == ""
如果price是空字符串,则为true
,否则为false
。
price == 0
如果price是整数0
,字符串"0"
或空字符串,则为true
,否则为false
。 如果要在price
为整数0
时捕获,则应将此比较更改为price === 0
0
。
price == null
这是true
,如果价格被传递给你的函数,是类型的null
,否则为false。
price == undefined
注意:您应该通过price === undefined
进行此比较,以查看price
是否undefined
且类型为undefined
。
这是true
,如果价格不传递给你的函数,或者如果价格是另有undefined
和false
,否则。
我建议只做整个条件语句!price
:
function validPrice(price) {
if (!price) {
//do something
}
else {
// do something different
}
};
var priceNew = $("li#listedProd").attr("price");
validPrice(priceNew);
EDIT添加了定义
JavaScript有2个用于测试是否相等的运算符(相反,有2个用于测试不平等的运算符。)
严格的相等比较(例如,===)仅在操作数具有相同类型并且如果对象实际上是同一对象,或者基元具有相同值(例如,0 === 0,和')时才为真。 a'==='a'都是真的)。 *注意NaN的这条规则略有例外(见下文)
在进行严格的相等比较之前, 抽象相等比较(例如==)将操作数转换为相同的类型(如果它们尚未存在)。 然而,如果一个操作数为null
或undefined
,当且仅当另一个操作数是比较虚null
或undefined
。
因此,简而言之, ==
等式运算符被许多人认为是不安全的。 要查看差异结帐http://dorey.github.io/JavaScript-Equality-Table/
例如'' ==
: 0, '', [], false, [[]]
0 ==
: false, 0, '0', '', [], [[]], [0]
null ==
: null, undefined
最后undefined ==
: null, undefined
最好使用strict equal ===
或者只测试一个falsey值( '', 0, undefined, null, false
)
if (!price) { //do something }
详细信息来自ECMA-262,第5版11.9.1 严格相等算法摘要
1. If typeof(x) is different from typeof(y), return false. 2. If typeof(x) is undefined, return true. 3. If x is null, return true. 4. If typeof(x) is number, then a. If x is NaN, return false. b. If y is NaN, return false. c. If x is the same Number value as y, return true. d. If x is +0 and y is -0, return true. e. If x is -0 and y is +0, return true. f. Return false. 5. If typeof(x) is string, then a. If x and y are exactly the same sequence of characters (same length and same characters in corresponding positions), return true. b. Else, return false. 6. If typeof(x) is boolean, then a. If x and y are both true or both false, return true. b. Else, return false. 7. If x and y are the same Object value, return true. 8. Return false.
并且...细节来自ECMA-262,第5版11.9.1 摘要平等算法摘要
1. If typeof(x) is the same as typeof(y), then return the result of performing strict equality comparison algorithm x === y. 2. If x is null and y is undefined, return true. 3. If x is undefined and y is null, return true. 4. If typeof(x) is number and typeof(y) is string, return the result of the comparison x == Number(y). 5. If typeof(x) is string and typeof(y) is number, return the result of the comparison Number(x) == y. 6. If typeof(x) is boolean, return the result of the comparison Number(x) == y. 7. If typeof(y) is boolean, return the result of the comparison x == Number(y). 8. If typeof(x) is either string or number and typeof(y) is object, return the result of the comparison x == [[ToPrimitive]](y). 9. If typeof(x) is object and typeof(y) is either string or number, return the result of the comparison [[ToPrimitive]](x) == y. 10. Return false. [[ToPrimitive]] is an internal function call
项目符号8和9基本上意味着对象的转换类似于object.valueOf()。toString()所以:
{} == '[Object object]' { hi: 'hi'} == '[Object object]' { valueOf: function(){ return 0; }} == 0 { toString: function(){ return 'hi'; }} == 'hi' { valueOf: function(){ return 0; }, toString: function(){ return 'hi'; }} == 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.