function testCount(count) {
console.log(count);
if (count === void 0) { count = 0; }
console.log(count);
}
其中if语句里的条件和typeof count ==='undefined' 完全一样吗?谁可以解释下? 这是哪个版本的语法?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
是完全一样的。
void
操作符的作用是:运算后面的表达式(为了处理运算符优先级问题,一般用小括号包裹起来),并返回undefined(这是原始值,不是字符串哦)
所以,上面的代码中的判断语句,相当于:
void 运算符
这里:
if (count === void 0) { count = 0; }
和if (count === undefined) { count = 0; }
或者if (typeof count === 'undefined') { count = 0; }
没什么本质区别。
var a,b;
alert(a === b)
道理是一样的
我去~给我-1了 …… 难道不都看本质吗
typeof count ==='undefined'
两个字符串‘undefined’相比较
count === void 0
两个‘undefined类型’相比较