/**
* 返回一个对象的类型
*/
function getType(x) {
//不能!x,因为"",0,false
if (null == x)
return "null";
//注意:typeof对j引用类型对返回object
var t = typeof x;
if (t != "object")return t;
//有的对象可能重写了toString方法,所以需要调用对象最原始的toString()
var s = Object.prototype.toString.call(x),
s = s.substring(8, s.length - 1);
//注意:==“Object”的时候需要判断具体的类型。
if (s != "Object")return s;
var x_constructor = x.constructor;
//x.constructor:即创建x对象的方法
if (x_constructor == Object)return s;
//一个对象的构造函数的原型对象中如定义有className属性(原本不存在的,这是用户您自己定义的属性),则返回这个属性的值
// function X(x,y,z){
// this.x=x;
// }
// X.prototype.className="X";
// var x=new X(1,2,3);
// alert(x.constructor.prototype== X.prototype)//return true.
var x_cons_proto = x_constructor.prototype;
if ("className" in x_cons_proto && typeof x_cons_proto.className == "string") {
return x_cons_proto.className;
}
//返回构造函数的名称
//但是只对于使用函数定义方式声明的函数有效,如:
//function ClassA(){this.x="ss"} var a=new ClassA(); alert(getType(a));
if ("name" in x_constructor && "" != x_constructor.name)return x_constructor.name;
/*下面都是闭包的情况*/
//对与调用Function构造的方式创建的对象,则
var c = x_constructor.toString(), c = c.substring(8, c.indexOf("(")).replace(/(^\s*)|(\s*$)/g, "");
//对于new方法直接量方式创建的对象,则暂时无能为力
if ("" == c) {
return "The object created by a anonymous function.";
}
return c;
}
/**
* 测试
*/
var printFunc = function() {
//--------------------------------------------------------------
//--------------------------------------------------------------
var k; //<br>
document.writeln(getType(k));
k = "";
document.writeln(getType(k));
k = 23;
document.writeln(getType(k));
k = true;
document.writeln(getType(k));
k = new Date();
document.writeln(getType(k));
k = /\s/;
document.writeln(getType(k));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
function Outer(x) {
var x = x;
//... ...
return function() {
return x;
}
}
document.writeln(getType(Outer));
var o = Outer(98);
document.writeln(getType(o));
var o = new Outer;
document.writeln(getType(o));
var o = new Outer(33);
document.writeln(getType(o));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
function Outer2(x) {
this.x = x;
//... ...
this.getX = function() {
return x;
}
}
document.writeln(getType(new Outer2(99)));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
function Outer3(x) {
this.x = x;
//... ...
this.getX = function() {
return x;
}
return x;
}
document.writeln(getType(new Outer3(92)));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
function MyClass(x, y) {
this.x = x;
this.y = y;
}
//MyClass.prototype.className = "MyClass"; //自定义一个属性,表示类型,则会提高执行效率
//MyClass.prototype.constructor = MyClass;
MyClass.prototype.getSum = function() {
return this.x + this.y;
};
var mc = new MyClass(2, 5);
document.writeln(mc.getSum());
document.writeln(getType(mc));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
var MyClass3 = function(x, y) {
this.x = x;
this.y = y;
}
//MyClass.prototype.className = "MyClass";
//MyClass.prototype.constructor = MyClass;
MyClass3.prototype.getSum = function() {
return this.x + this.y;
};
mc = new MyClass3(1, 5);
document.writeln(mc.getSum());
document.writeln(getType(mc));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
//--------------------------------------------------------------
//--------------------------------------------------------------
var MyClass2 = Function("x", "y", "this.x = x;this.y = y;");
MyClass2.prototype.getSum = function() {
return this.x + this.y;
};
mc = new MyClass2(3, 5);
document.writeln(mc.getSum());
document.writeln(getType(mc));
}
printFunc();
//document.writeln("<br>");
//document.writeln(printFunc);
分享到:
相关推荐
我们知道,JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串。如:”number”,”string”,”...
我们能够使用typeof判断变量的身份,判断字符串得到string,数字和NaN得到number,函数会得到function等,但是判断数组,对象和null时都会得到object,详细请看js数据类型,这就是typeof的局限性,并不能准确的判断...
主要介绍了如何判断JavaScript中对象的类型,举例讲解了使用typeof和instanceof等操作符来进行判断的方法,需要的朋友可以参考下
文中为大家分享了三种JavaScript判断对象是否为数组的方法, 1. typeof 首先我们会想到的是使用typeof来检测数据类型,但是对于Function, String, Number, Undefined等这几种基本类型来说,使用typeof来检测都可以...
我们知道,JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性
主要介绍了JavaScript中判断变量是数组、函数或是对象类型的方法,需要的朋友可以参考下
一个可以直接使用的判断数据类型的js文件,里面整理了比较全面的判断各种数据类型分方法,前端的小伙伴看过来
8、原生JavaScript判断是否为数字类型 9、原生JavaScript设置cookie值 10、原生JavaScript获取cookie值 11、原生JavaScript加入收藏夹 12、原生JavaScript设为首页 13、原生JavaScript判断IE6 14、原生JavaScript...
javascript中经常使用的对象判断方式包括:typeof、constructor和Object.prototype.toString.call()最近在翻看JohnResig的大作《ProJavaScriptTechniques》,里面讲到了如何做javascript的类型判断的问题。...
主要介绍了JavaScript之Blob对象类型的具体使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
我们知道,JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性
在开发中,我们经常需要判断某个对象是否为数组类型,在Js中检测对象类型的常见方法都有哪些呢?
javascript 判断一个对象为数组的方法 数组对象 js的数组是无类型的:数组元素可以是任意类型,并且同一个数组中的不同元素也可能有不同的类型。数组的元素可以是对象或其他数组,这样就可以创建复杂的数据结构。 ...
在JavaScript中所有数据类型严格意义上都是对象,但实际使用中我们还是有类型之分,如果要判断一个变量是数组还是对象使用typeof搞不定,因为它全都返回object 代码如下: var o = { ‘name’:’lee’ }; var a = ...