javascript类型识别


typeof总结:

typeof属于操作符;

typeof 1;//"number"

typeof (1);//"number"

.可以识别标准类型(Null除外)

.不能识别具体的对象类型(Function 除外)

instanceof总结:

.判别内置对象类型

[] instanceof Array; // true

/\d/ instanceof RegExp; // true

.不能判别原始类型

1 instanceof Number; // false

"jerry" instanceof String; //false

//能够判别自定义对象类型及父子类型

function Point(x,y){

this.x = x;

this.y = y;

}

function Circle(x,y,r){

Point.call(this,x,y);

this.radius = r;

}

Circle.prototype =new Point();

Circle.prototype.constructor = Circle;

var c = new Circle(1,1,2);

c instanceof Circle // true

c instanceof Point // true

Object.prototype.toString.call

function type(obj){

return Object.prototype.toString.call(obj).slice(8,-1);

}

.可以识别标准类型以及内置(build-in)对象类型

type(1)//"Number"

type(new Date) //"Date"

.不能识别自定义对象类型

function Point(x,y){

this.x = x;

this.y = y

}

type(new Point(1,2)); // "Object"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Javascript 中有6种基本类型(包括 symbol),以及对象类型,他们在不同的运算中会被系统转化为不同是...
    faremax阅读 4,336评论 0 3
  • ECMAScript关键字 delete do else finally function in instance...
    doudou2阅读 3,996评论 0 0
  • 记最近有感 我不是说爱情,我是说友情。 最近的状态,实在是令我自己都讨厌的厉害。我病了,病的深入骨髓,不能自已。我...
    阿衡挺能闹阅读 3,471评论 0 0
  • 缺失值简介 造成数据缺失的原因 有些信息暂时无法获取。例如小越现在在看哪个小姐姐 有些信息是被遗漏的。可能是因为输...
    1想得美阅读 18,788评论 1 12
  • 喜欢 1、与徐总互动,有智者,心有灵犀。 2、群内分享,得到吴总的回流。 3、晚上,分享股权,感觉真是太棒了,比单...
    雄安区阅读 1,864评论 0 1

友情链接更多精彩内容