晴明的博客园 GitHub      CodePen      CodeWars     

[js] typeof

typeof 

typeof operand

operand 是一个表达式,表示对象或原始值,其类型被返回。

typeof操作符返回一个字符串,表示未经求值的操作数(unevaluated operand)的类型。

 

            // Numbers
            typeof 37 === 'number';
            typeof 3.14 === 'number';
            typeof Math.LN2 === 'number';//属性
            typeof Infinity === 'number';
            typeof NaN === 'number'; 
            // 尽管NaN是"Not-A-Number"的缩写,意思是"不是一个数字"
            typeof Number(1) === 'number'; 
            // 不要这样使用!
            
            // Strings
            typeof "" === 'string';
            typeof "bla" === 'string';
            typeof (typeof 1) === 'string'; 
            // typeof返回的肯定是一个字符串
            typeof String("abc") === 'string'; 
            // 不要这样使用!
            
            // Booleans
            typeof true === 'boolean';
            typeof false === 'boolean';
            typeof Boolean(true) === 'boolean'; 
            // 不要这样使用!
            
            // Symbols
            // #ES6
            typeof Symbol() === 'symbol';
            typeof Symbol('foo') === 'symbol';
            typeof Symbol.iterator === 'symbol';
            
            // Undefined
            typeof undefined === 'undefined';
            typeof blabla === 'undefined'; 
            // 一个未定义的变量,或者一个定义了却未赋初值的变量
            
            // Objects
            typeof {a:1} === 'object';
            
            // 使用Array.isArray或者Object.prototype.toString.call方法可以从基本的对象中区分出数组类型
            typeof [1, 2, 4] === 'object';
            
            typeof new Date() === 'object';
            
            // 下面的容易令人迷惑,不要这样使用!
            typeof new Boolean(true) === 'object';
            typeof new Number(1) === 'object';
            typeof new String("abc") === 'object';
            
            // 函数
            typeof function(){} === 'function';
            typeof Math.sin === 'function';        
            
            // null
            // 从JavaScript一开始出现就是这样的 
            typeof null === 'object';
            
            //正则表达式
            typeof /s/ === 'function'; 
            // Chrome 1-12 , 不符合 ECMAScript 5.1
            typeof /s/ === 'object'; 
            // Firefox 5+ , 符合 ECMAScript 5.1

 

posted @ 2016-02-28 01:17  晴明桑  阅读(146)  评论(0编辑  收藏  举报