1、示例代码
1.1、加(+)运算
console.log(undefined + undefined);
console.log(undefined + null);
console.log(null + undefined);
console.log(undefined + NaN);
console.log(NaN + undefined);
console.log(undefined + true);
console.log(true + undefined);
console.log(undefined + false);
console.log(false + undefined);
console.log(undefined + 1);
console.log(1 + undefined);
console.log(undefined + 0);
console.log(0 + undefined);
console.log(undefined + 2);
console.log(2 + undefined);
console.log(undefined + 'true');
console.log('true' + undefined);
console.log(undefined + '0');
console.log('0' + undefined);
console.log(undefined + []);
console.log([] + undefined);
console.log(undefined + [1]);
console.log([1] + undefined);
console.log(undefined + [1, 2]);
console.log([1, 2] + undefined);
console.log(undefined + {});
console.log({} + undefined);
console.log(undefined + { names: '' });
console.log({ names: '' } + undefined);
console.log(undefined + { names: '159' });
console.log({ names: '159' } + undefined);
1.2、减(-)运算
console.log(undefined - undefined);
console.log(undefined - null);
console.log(null - undefined);
console.log(undefined - NaN);
console.log(NaN - undefined);
console.log(undefined - true);
console.log(true - undefined);
console.log(false - undefined);
console.log(undefined - 1);
console.log(1 - undefined);
console.log(0 - undefined);
console.log(2 - undefined);
console.log(undefined - 'true');
console.log('true' - undefined);
console.log(undefined - '0');
console.log('0' - undefined);
console.log(undefined - []);
console.log([] - undefined);
console.log(undefined - [1]);
console.log([1] - undefined);
console.log(undefined - [1, 2]);
console.log([1, 2] - undefined);
console.log(undefined - {});
console.log({} - undefined);
console.log(undefined - { names: '' });
console.log({ names: '' } - undefined);
console.log({ names: '159' } - undefined);
2、总结
1、undefined与undefined ||null ||NaN ||boolean ||number 类型做加(+)运算,不分先后,结果都为:NaN 。 2、undefined与字符串 进行加(+)运算,不分先后,从左往右按字符串 相加。 3、undefined与数组 进行加(+)运算时会调用.toString() 方法,不分先后,结果为:''||'1,2'||'1,2,3' ,并从左往右按字符串 相加。 4、undefined与对象 进行加(+)运算时会调用.toString() 方法,不分先后,结果为:[object Object] ,并从左往右按字符串 相加。 5、undefined与任何类型的数据进行减(-)||乘(*)||除(/)||取模(%) 运算,不分先后,结果都为:NaN 。
|