JS中的类型识别

JS为弱类型语言,所以类型识别对JS而言尤为重要,JS中常用的类型识别方法有4种:typeof、Object.prototype.toString、constructor和instanceof。

(1)typeof可以识别标准类型(Null除外),不能识别具体的对象类型(Function除外),举例如下:

1 console.log(typeof("tom"));  //"string"
2 console.log(typeof(12)); //"number"
3 console.log(typeof(true)); //"boolean"
4 console.log(typeof(undefined)); //"undefined"
5 console.log(typeof(null)); //"objectl" null被识别为object
6 console.log(typeof({name:"yxz"})); //"object"
7 console.log(typeof(function(){})); // "function"

(2)Object.prototype.toString可以识别标准类型以及内置对象类型,不能识别自定义类型,举例如下:

先封装一个函数实现Object.prototype.toString功能。

 1 function type(obj){
 2     return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
 3 }
 4 console.log(type(1));//"number"
 5 console.log(type("abc"));//"string"
 6 console.log(type(true));//"boolean"
 7 console.log(type(undefined));//"undefined"
 8 console.log(type(null));//"null"
 9 console.log(type({}));//"object"
10 console.log(type(new Date));//"date"
11 console.log(type(function(){}));//"funciton"
12 console.log(type([]));//"array"
13 function Point(x,y){
14     this.x = x;
15     this.y = y;
16 }
17 console.log(type(new Point(1,2)));//"object" 自定义类型不能识别

(3)constructor是对象原型上面的一个属性,它指向构造器本身,constructor可以识别标准类型(Undefined/Null除外),可以识别内置对象类型,可以识别自定义对象类型,举例如下:

 1 //标准类型
 2 console.log("tom".constructor == String);//true
 3 console.log((1).constructor == Number);//true
 4 console.log(true.constructor == Boolean);//true
 5 console.log({}.constructor == Object);//true
 6 //内置类型对象
 7 console.log([].constructor == Array);//true
 8 //自定义对象
 9 function Person(name){
10     this.name = name;
11 }
12 console.log(new Person("tom").constructor == Person);//true

(4)instanceof可以识别内置对象类型,不能识别原始类型,可以识别自定义对象类型,举例如下:

 1 //识别内置对象类型
 2 console.log([] instanceof Array);//true
 3 //不能识别原始类型
 4 console.log(1 instanceof Number);//false
 5 console.log("tom" instanceof String);//false
 6 //能识别自定义对象类型及父子类型
 7 function Point(x,y){
 8     this.x = x;
 9     this.y = y;
10 }
11 function Circle(x,y,r){
12     Point.call(this,x,y);
13     this.radius = r;
14 }
15 Circle.prototype = new Point();
16 Circle.prototype.constructor = Circle;
17 var c = new Circle(1,1,2);
18 console.log(c instanceof Circle);//true
19 console.log(c instanceof Point);//true
时间: 2024-08-29 07:54:11

JS中的类型识别的相关文章

JS中的类型检测

JS中用于类型检测的函数有typeof.instanceof .Object.prototype.toString.constrcutor.duck type typeof用于检测基本类型和函数 有些特殊情况 null.数组.Date数据类型用typeof判断返回的是object instanceof用来判断对象类型,基于原型链,可以用来判断数组和Date数据类型

推断js中的类型:typeof / instanceof / constructor / prototype

怎样推断js中的类型呢,先举几个样例: var a = "jason"; var b = 123; var c = true; var d = [1,2,3]; var e = new Date(); var f = function(){ alert('jason'); }; 一.最常见的推断方法:typeof typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果,另外typeof能够推断function的类型:在推断除Object类型的对象时

JS中基本类型与包装类型的关系

对于JS中一些类型的转化的东西,自己测试并得出的结论,有错误的地方请大大们留言. 不多废话,直接贴代码,测试请直接拷贝全部代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>E

js中的类型和函数参数传递类型问题

js中的类型: 2大类型:原始类型和对象. 原始类型有 boolean.number.string这三个普通原始类型,还有null.undefined这俩特殊原始类型 对象嘛就多了,普通对象.内置对象.全局对象.函数.数组等. 函数参数传递类型: 对于原始类型,传递的是值,即复制一份传入函数,在函数内部修改不影响外部变量本身. 对于对象类型,传递的是地址,在函数内部修改对象时会导致外部变量发生变化. 注意这种情况!参考如下代码: var oMyObj = {name:"罗伯特"}; f

判断js中的类型:typeof / instanceof / constructor / prototype

如何判断js中的类型呢,先举几个例子: var a = "jason"; var b = 123; var c = true; var d = [1,2,3]; var e = new Date(); var f = function(){ alert('jason'); }; 一.最常见的判断方法:typeof typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果,另外typeof可以判断function的类型:在判断除Object类型的对象时

js中boolean类型的理解

<html> <head> <script type="text/javascript"> var x="12"; alert(x+234); alert(typeof (x+234)); var flag=false; //true=1 false=0; alert(flag+34); </script> </head> <body> String str="ere"; i

js 中的类型比较

console.log(typeof 1) // number console.log(typeof 'cc') // string console.log(typeof true) // boolean console.log(typeof undefined) // undefined console.log(typeof null) // object console.log(typeof NULL) // undefined NULL | Null 都不对 console.log(typ

第66课 C++中的类型识别

1. 类型识别 (1)在面向对象中可能出现下面的情况 ①基类指针指向子类对象 ②基类引用成为子类对象的别名 ▲静态类型——变量(对象)自身的类型(定义变量类型时类型或参数类型) ▲动态类型——指针(引用)所指向的对象的实际类型 (2)基类指针转子类指针: ①示例:Derived* d = static_cast<Derived*>(pBase); //危险的转换方式 ②问题:不安全,是否能强制类型转换取决动态类型. 2. 利用多态获取动态类型 (1)解决方案 ①在基类中定义虚函数,并返回具体的

js中基本类型的值和引用类型的值的比较

1.动态属性方面的比较 1)基本类型的值是不能添加属性的,即使不会产生任何错误: 2)引用类型的值可以动态的添加属性,以便后面的使用: 1 var cat='cat'; 2 cat.name='Tom'; 3 console.log(cat.name);//undefined 4 5 6 var mouse={}; 7 mouse.name='Jerry'; 8 console.log(mouse.name);//Jerry 2.变量值复制方面的比较 1)基本类型复制的是原始值,不同变量之间不会