typeof

typeof是一个一元运算符,它返回的结果 始终是一个字符串,对不同的操作数,它返回不同的结果。

此表总结了typeof所有可能的返回值:
操作数类型 返回值
undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
函数对象 "function"
E4X XML 对象 "xml"
E4X XMLList 对象 "xml"
其他对象 "object"
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"

// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof always return a string

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';

// Undefined
typeof undefined === 'undefined';
typeof blabla === 'undefined'; // an undefined variable

// Objects
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object'; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
typeof new Date() === 'object';

typeof new Boolean(true) === 'object'; // this is confusing. Don't use!
typeof new Number(1) === 'object'; // this is confusing. Don't use!
typeof new String("abc") === 'object';  // this is confusing. Don't use!

// Functions
typeof function(){} === 'function';
typeof Math.sin === 'function';

typeof undefined;//"undefined"
typeof null;//"object" This stands since the beginning of JavaScript
typeof /s/ === 'object'; // Conform to ECMAScript 5.1

constructor

JavaScript中,每个对象都有一个constructor属性,它引用了初始化该对象的构造函数,常用于判断未知对象的类型。如给定一个求知的值通过typeof运算符来判断它是原始的值还是对象。如果是对象,就可以使用constructor属性来判断其类型。

/*
* 通过constructor判断对象是否为Array对象
*/
var array = new Array();
array.constructor === Array; // true

instanceof

通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。

// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例
function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型继承

var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true

上面的代码表示,在多层继承关系中,instanceof 运算符同样适用。

以下代码为 instanceof 代码实现。

/** JavaScript instanceof 运算符代码 **/
function instance_of(L, R) {//L 表示左表达式,R 表示右表达式
   var O = R.prototype;// 取 R 的显示原型
   L = L.__proto__;// 取 L 的隐式原型
   while (true) {
     if (L === null)
       return false;
     if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true
       return true;
     L = L.__proto__;
   }
}
console.log(Object instanceof Object);//true
console.log(Function instanceof Function);//true
console.log(Number instanceof Number);//false
console.log(String instanceof String);//false 

console.log(Function instanceof Object);//true 

console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false

注:

通过instanceof 和 constructor 在检测在跨框架(cross-frame)页面中的对象时,会失败。

原因就是在不同框架(iframe)中创建的数组不会相互共享其prototype属性。

正确的检测方法为:

var x = "";
Object.prototype.toString.call(x); // "[object String]"

var x = 1;
Object.prototype.toString.call(x); // "[object Number]"

var x = NaN;
Object.prototype.toString.call(x); // "[object Number]"

var x = Infinity;
Object.prototype.toString.call(x); // "[object Number]"

var x = new Array();
Object.prototype.toString.call(x); // "[object Array]"

var x = new Object();
Object.prototype.toString.call(x); // "[object Object]"

var x = new Date();
Object.prototype.toString.call(x); // "[object Date]"

var x = new Error();
Object.prototype.toString.call(x); // "[object Error]"

var x = RegExp();
Object.prototype.toString.call(x); // "[object RegExp]"

var x = undefined;
Object.prototype.toString.call(x); // "[object Undefined]"

var x = null;
Object.prototype.toString.call(x); // "[object Null]"
Object.prototype.toString.call(arguments); // "[object Arguments]"

jquery所提供的判定方法:

jQuery.isArray(x); //判定是否为数组
jQuery.isWindow(x); //判断是否为window对象,当前窗口和iframe
jQuery.isNumeric(x); //判定是否为数字 NaN,Infinity,""...false
jQuery.isFunction(x); //判定是否为javascript函数

最新文章

  1. Web 前端之HTML和CSS
  2. 聊下 git rebase -i
  3. java 读取文件内容 三种形式及效率对比
  4. 飞思卡尔9S12X系列双核中的协处理器XGATE使用方法
  5. CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装
  6. UVa 1328 (KMP求字符串周期) Period
  7. MSSQLSERVER数据库- 慎用SELECT INTO复制表
  8. JQuery上传插件Uploadify API详解
  9. STM32F030 IO口外部中断应用
  10. jQuery 使用 jQuery UI 部件工厂编写带状态的插件(翻译)
  11. JavaWeb显示器
  12. ERROR 1406 : Data too long for column 解决办法
  13. [入门向选讲] 插头DP:从零概念到入门 (例题:HDU1693 COGS1283 BZOJ2310 BZOJ2331)
  14. 【bzoj2761】[JLOI2011]不重复数字
  15. 主函数特别之处:public static void main(String[] args)
  16. vue v-for输出表格结构
  17. UVA1601-The Morning after Halloween(双向BFS)
  18. WxWidgets笔记
  19. 人生苦短之---认识Python
  20. 解决在使用pip list时出现DEPRECATION

热门文章

  1. Leetcode_206_Reverse Linked List
  2. CAS实现单点登录--错误记录
  3. 【Android 应用开发】BluetoothAdapter解析
  4. Android Binder IPC详解-Android学习之旅(96)
  5. 遍历输出图片加hover
  6. 面试题-一个for循环输出一个棱形
  7. spring 整合 mybatis 中数据源的几种配置方式
  8. Day24 中间件 自定义分页 ModelForm 序列化 缓存 信号
  9. Ubuntu16.04部署phantomjs的一个问题
  10. 关于Django升级的一些联想