js 中的类型比较

console.log(typeof 1)            // number
console.log(typeof 'cc')        // string
console.log(typeof true)        // boolean
console.log(typeof undefined)     // undefined
console.log(typeof null)        // object
console.log(typeof NULL)         // undefined  NULL | Null 都不对
console.log(typeof {})            // object
console.log(typeof [])            // object
console.log(typeof function(){})  // function 这个有点意料之外,本以为也是 object
console.log(typeof Symbol())     // symbol

// 以下:皆 true
console.log(typeof true < typeof function(){} ? true : false); 
console.log(
typeof function(){} < typeof 1 ? true : false);
console.log(
typeof 1 < typeof {name:'cc'} ? true : false);
console.log(
typeof {name:'cc'} === typeof null ? true : false);
console.log(
typeof {name:'cc'} < typeof 'cc' ? true : false);
console.log(typeof 'cc' < typeof Symbol() ? true : false); console.log(
typeof Symbol() < typeof undefined ? true : false);

// 基于多键值进行排序的时候或许可以用一下
// boolean < function < number < object(null) < string < symbol < undefined

// 负责任的揭秘一下: a b c d e f g h i j k l m n o p q r s t u v w x y z
// 最后补充:不知道该被自己逗死,还是蠢死.. 誒
console.log( typeof typeof [] ) // string

 

  • typeof 返回值是小写
  • null 小写,NULL Null 这些写法不对
  • 函数 typeof 返回值是 function。疑惑脸:什么时候 function 是 js 的基本类型了?
  • 这样说吧: typeof  是一个运算符,返回结果并不等同于 js 基本类型 (null 不等, function 看来也是)
  • js 中基本类型:
    boolean
    number
    object (Object | Function | Array | RegExp | ..)
    null
    string
    undefined
    symbol (ES6 新增)
  • typeof 的返回值:
    boolean  
    number
    string object (object | array | null)
    function
    undefined symbol

 

posted @ 2017-12-22 12:04  弋儿  阅读(821)  评论(0编辑  收藏  举报