获取javascript变量类型的方法:1、使用typeof操作符,语法“typeof 变量”;2、使用jquery的“$.type()”方法;3、通过构造函数来获取类型。
本教程操作环境:windows7系统、javascript1.8.5&&jquery1.10.0版、dell g3电脑。
在javascript中,如何准确获取变量的类型名是一个经常使用的问题.
但是常常不能获取到变量的精确名称,或者必须使用jquery 中的方法,这里 我通过 typeof ,jquery.type 和 通过构造函数来获取变量类型 这三种方法详细介绍一遍.
希望可以对你提供帮助.
看到题目的第一眼,有些同学可能会想到 typeof 运算符.
使用 typeof 获取基本的类型在javascript语言中,给出了使用 typeof 运算符来获取基本的类型名.(注意不是基本类型)
这是 typeof 的全部用法
01-typeof.htm
console.log('typeof of 10 ~~~~' +typeof 10);console.log('typeof of "a" ~~~~' +typeof 'a');console.log('typeof of true ~~~~' +typeof true);console.log('typeof of {} ~~~~' +typeof {});console.log('typeof of /123/ ~~~~' +typeof /123/);console.log('typeof of function(){} ~~~~' +typeof function(){});console.log('typeof of undefined ~~~~' +typeof undefined);console.log('typeof of null ~~~~' +typeof null);
这是结果
按照上面的打印结果,总结出下面要注意的几点
typeof (引用类型) 除了函数, 都是 'object',比如 typeof /123/
typeof null 为'object'
typeof undefined 为 'undefined',通常, 如果使用两等号, null == undefined 为真.
转换为数字的常见用法 "10"-0, 如果没有转换成功,返回nan,由于nan 的一个特性: nan != nan,故判断转换成功与否的常见做法: (这也是我参见 jquery的源码发现的,jquery源码读100遍都不为过)
("10x" - 0) == ("10x" - 0); // 结果为假!
使用jquery中的方法$.type()现在看看jquery是怎么做的
// 先申明一个对象,目的是用来做映射var class2type = {};// 申明一个core_tostring() 的方法,得到最原始的tostring() 方法,因为在很多对象中,tostrintg() 已经被重写 var core_tostring() = class2type.tostring;
// 这里为 tostrintg() 后的结果和类型名做一个映射,申明一个core_tostring() 后的结果,而值就是类型名jquery.each("boolean number string function array date regexp object error".split(" "), function(i, name) { class2type[ "[object " + name + "]" ] = name.tolowercase();});
因为 object.prototype.tostring() 方法调用结果如下
var core_tostring = {}.tostring;console.log( core_tostring.call(1) );console.log( core_tostring.call("11") );console.log( core_tostring.call(/123/) );console.log( core_tostring.call({}) );console.log( core_tostring.call(function(){}) );console.log( core_tostring.call([]) );console.log( core_tostring.call(true) );console.log( core_tostring.call(new date()) );console.log( core_tostring.call(new error() ));console.log( core_tostring.call(null) );console.log( core_tostring.call(undefined) );console.log( string(null) );console.log( string(undefined) );
上面的打印结果与
class2type[ "[object " + name + "]" ] = name.tolowercase();
不谋而合!
这是jquery.type 的核心方法
type: function( obj ) { if ( obj == null ) { return string( obj ); } // support: safari <= 5.1 (functionish regexp) return typeof obj === "object" || typeof obj === "function" ? class2type[ core_tostring.call(obj) ] || "object" : typeof obj;},
注意,为什么把 null 或者 undefined 单独讨论呢,因为 在一些版本浏览器中
console.log(core_tostring.call(null));console.log(core_tostring.call(undefined));
这是会报错的!
如果是对象类型,另:由于 在一些低版本的浏览器中,typeof /123/ 会返回的是 "function" 而不是 "object",所以这里要判断是否是函数,要明白 这里的 typeof obj === function 不是为了函数讨论的,因为函数本身就可以通过typeof 来得到类型.
typeof obj === "object" || typeof obj === "function" ? class2type[ core_tostring.call(obj) ]
就直接返回class2type 中键值对的结果,,如果不是,那么一定就是基本类型, 通过 typeof 就可以啦.
class2type[ core_tostring.call(obj) ] || "object" :// 这是防止一些未知情况的,如果未取到,就返回object
但是 jquery.type 有一个很大的缺陷
这是一个自定义类型
function person(){ this.name = 'pawn';}var p = person.tostring();
// 注意,这里会打印 [object object],通过上面的方法,无法得到精确的自定义类型
这也是 它的一个大缺陷了!
下面,我们通过构造函数的方式来获取精确类型
通过构造函数来获取类型在理解这个方法之前,需要理解两个点
prorotype 原型属性我们知道,任何对象或者函数都直接或者间接的继承自object 或者 function, (其实最终function 是继承自 object 的,这属于原型链的知识了)。那么,任何一个对象都具有原型对象 __proto__ (这个对象只在chrome 和 firefox 暴露,但是在其他浏览器中也是存在的),这个原型对象就是这个对象的构造函数的原型属性(这里可能有点绕).
由于 任何函数都具有 原型属性prototype,并且这个原型属性具有一个默认属性 constructor,它是这个函数的引用,看下面的代码
function person(){ this.name = 'pawn'; } console.log(person.prototype.constructor === person);
发现,这两个东西其实一个东西
但是,在某些情况下,需要这么写
function person(){ this.name = 'pawn'; } person.protype = { xx: ... , xx: ... , ... }
这么做,就会覆盖原本的 protype 方法,那么construcor 就不存在了,这是,必须要显示的申明这个对象
person.protype = { construction: person, xx: ... , xx: ... , ... }
在jquery的中,就是这么做的,
jquery.fn = jquery.prototype = { constructor: jquery, init: function( selector, context, rootjquery ) { var match, elem;
关于 jquery对象封装的方式 也是非常值得研究
function.prototype.tostring()
注意,这里已经不是熟悉 [object object],而是 已经重写了.
也就是,如果调用一个函数的tostring() 方法.那么就会打印这个函数的函数体.
好了,经过上面两个步骤,你明白我要做什么了吗?
如何通过构造函数来获得变量的类型?
判断是否是基本类型 var gettype = function(obj){ if(obj == null){ return string(obj); } if(typeof obj === 'object' || typeof obj === 'fucntion'){ ... }else{ // 如果不是引用类型,那么就是基本类型 return typeof obj } }
如果是对象或者函数类型 function person(){ this.name = 'pawn'; } var p = new person(); console.log(p.constructor);
现在要做的事 : 如何将person 提取出来呢?
毋庸置疑,字符串切割那一套肯定可以办到,但是太 low 啦!
这里,我使用正则将person提取出来
var regex = /function\s(.+?)\(/ function person(){ this.name = 'pawn'; } var p = new person(); var c = p.constructor var regex = /function\s(.+?)\(/; console.log('|' + regex.exec(c)[1] + '|');
使用name其实,除了上面的正则,每个函数还有一个name属性,返回函数名,但是ie8 是不支持的.
因此上面的代码可以写为:
var gettype = function(obj){ if(obj == null){ return string(obj); } if(typeof obj === 'object' || typeof obj === 'function'){ var constructor = obj.constructor; if(constructor && constructor.name){ return constructor.name; } var regex = /function\s(.+?)\(/; return regex.exec(c)[1]; }else{ // 如果不是引用类型,那么就是基本;类型 return typeof obj; }};
但是上面的代码太丑啦,将其简化
简化var gettype = function(obj){ if(obj == null){ return string(obj); } if(typeof obj === 'object' || typeof obj === 'function'){ return obj.constructor && obj.constructor.name.tolowercase() || /function\s(.+?)\(/.exec(obj.constructor)[1].tolowercase(); }else{ // 如果不是引用类型,那么就是基本类型 return typeof obj; }};
还是比较麻烦,继续简化
var gettype = function(obj){ if(obj == null){ return string(obj); } return typeof obj === 'object' || typeof obj === 'function' ? obj.constructor && obj.constructor.name && obj.constructor.name.tolowercase() || /function\s(.+?)\(/.exec(obj.constructor)[1].tolowercase(): typeof obj;};
好了,已经全部弄完了,写个代码测试一下:
function person(){ this.name = 'pawn';}var p = new person();console.log(gettype(p));console.log(gettype(1));console.log(gettype("a"));console.log(gettype(false));console.log(gettype(/123/));console.log(gettype({}));console.log(gettype(function(){}));console.log(gettype(new date()));console.log(gettype(new error()));console.log(gettype( null));console.log(gettype( undefined));
【推荐学习:javascript高级教程】
以上就是如何获取javascript变量的类型的详细内容。