当使用 条件判断语句(if...else) 以及 布尔操作符(!、&&、||) 时,会调用Boolean()进行隐式类型转换
![]; // !Boolean([]); ------------> false !2; // false !null; // !Boolean(null); ------------> true !undefined; // true !0; // true if([]) consolo.log('true'); // if(Boolean([])) ... -----------> true
2 * '5'; // 10 2 * '5a'; // 2 * Number('5a') --------> 2 * NaN ------------> NaN
// num: 数值操作数 str: 字符串 obj: 对象 num + 非字符串操作数(NaN/undefined/null/false/true) = num + Number(非字符串操作数) num + str = 'numstr'; // 1 + 2 + '3' = '33' str + NaN = 'strNaN'; // '1' + NaN = '1NaN' str + null = 'strnull'; // '1' + null = '1null' str + true = 'strture'; obj + num = obj.toString() + num; // Math + 123 = '[object Math]123' obj + str = obj.toString() + str; // Math + '123' = '[object Math]123'
1 - '2' = -1; 1 - '123a' = NaN; // 1 - Numer('123a') 1 - Math = NaN; // 1 - Math.valueOf() 1 - true = 0;
4 > '2'; // true 4 > '2a'; // false 4 > Math; // false 4 > true; // true 'ab' > 'a' // true
// true 123 == '123'; 1 == true; '123' == new String(123); 123 == new String(123); null == undefined; // false null == 0; 2 == true; new String(123) == new String(123);
全等操作符不会转换操作数
123 === '123'; // false null === undefined; // false
你能说说下面结果是什么以及原因吗?
[] == 0; // 1.?? {} == 0; // 2.?? 0 == {}; // 3.?? [] == {}; // 4.??
根据相等操作符的规定,当一个操作数为对象另一个不是时,会调用对象的valueOf()
[] == 0; ---------> [].valueOf() -----> '' -----> Number('') ----> 0
当两个操作数都为对象则会比较它们是否指向同一块区域,而[]和{}是两个独立的对象,所以为false。
如对本文有疑问, 点击进行留言回复!!
教你搭建 nodejs+mongoose+Graphql+Vue+Typescript 框架(上)
【Nginx】如何为已安装的Nginx动态添加模块?看完我懂了!!
网友评论