10道题讲解

154 阅读1分钟
<script>
// !(!"Number(undefined)");
/* 
* ""  ''  `` 都是字符串   => "Number(undefined)"就是一个字符串
*   !"Number(undefined)" 转换为布尔在取反  => false
*   !false  => true
*/

// isNaN(parseInt(new Date())) + Number([1]) + typeof undefined;​
/*
* + 是最后计算的,先把每一项的值都算出来,最后相加
*     isNaN(parseInt(new Date()))  从最里层开始逐一向外层计算
*        new Date() => 当前本机日期(对象)
*        parseInt(对象) => parseInt("Fri Mar 06 2020 11:43:18 GMT+0800") =>NaN
*        isNaN(NaN) => true
*     Number([1]) 
*         Number("1") =>1
*     typeof undefined
*         =>"undefined"
* true + 1 + "undefined"
*    true + 1 => 2
*    2 + "undefined" => "2undefined"
*/


// Boolean(Number("")) + !isNaN(Number(null)) + Boolean("parseInt([])") + typeof !(null);​
/*
* Boolean(Number(""))
*    Number("") => 0
*    Boolean(0) => false
* !isNaN(Number(null))
*    Number(null) => 0
*    isNaN(0) => false
*    !false => true
* Boolean("parseInt([])")
*    "parseInt([])" 他是一个字符串,并不是转换数字啥的
*    => true
* typeof !(null)
*     !(null) => true
*     typeof true => "boolean"
* false + true + true + "boolean"  => "2boolean"
*/

// parseFloat("1.6px") + parseInt("1.2px") + typeof parseInt(null);​
/*
* parseFloat("1.6px") => 1.6
* parseInt("1.2px") => 1
* typeof parseInt(null)
*    parseInt(null) => parseInt('null') => NaN
*    typeof NaN => "number"
* 1.6 + 1 + "number" => '2.6number'
*/

// isNaN(Number(!!Number(parseInt("0.8"))));​
/*
* parseInt("0.8") => 0
* !!Number(0) => false
* Number(false) => 0
* isNaN(0) => false
*/

// console.log(1 + "2" + "2");​
/*
*  '122'
*/

// !typeof parseFloat("0");​
/*
*  parseFloat("0") => 0
*  typeof 0 => "number"
*  !"number" => false
*/

// Number("");​
/* 0 */

// typeof "parseInt(null)" + 12 + !!Number(NaN);​
/*
* typeof "parseInt(null)" => "string"
* 12
* !!Number(NaN) => false
* => "string12false"
*/

// !typeof (isNaN("")) + parseInt(NaN);​
/* 
* !typeof (isNaN(""))
*    isNaN("") => isNaN(Number("")) => isNaN(0) => false
*    typeof false => "boolean"
*    !"boolean" => false
* parseInt(NaN)
*    parseInt(String(NaN)) => parseInt('NaN') => NaN
* false + NaN => NaN
*/

// typeof !parseInt(null) + !isNaN(null);
/*
*  typeof !parseInt(null)
*     parseInt(null) => parseInt('null') => NaN
*     !NaN => true
*     typeof true => "boolean"
*  !isNaN(null)
*     isNaN(null) => isNaN(Number(null)) => isNaN(0) => false
*     !false => true
*  "boolean" + true => "booleantrue" 
*/
</script>