/* *以字符串形式返回值的类型 */ function type(o){ var t,c,n;//type, class, name if(o===null)return 'null'; if(o!==o)return 'nan'; //如果typeof值不是object,返回它的值 //可以识别出原始值的类型和函数 if((t=typeof(o))!=='object') return t; //如果classof值不是Object,返回它的值 //可以识别出大多数的内置对象 if((c=classof(o))!=='Object') return c; //如果构造函数的名字存在的话,发挥它 if(o.constructor && trypeof(o.constructor ==='function') && (n=o.constructor.getName())) return n; //其它类型无法识别,返回Object return 'object'; } //返回对象的类 function classof(o){ return Object.prototype.toString.call(o).slice(8,-1); } //返回对象的名字 Function.prototype.getName=function(){ //如果函数中有name属性就返回name的值,否则取'function'和'('之间的字符 return this.name || this.toString().match('/function\s*([^()*]\(')[1]; } document.writeln('fff');
Javascript 识别对象的类型
最新推荐文章于 2023-01-01 15:31:51 发布