摘要:类型系统搬运自个人博客原址类型识别类型系统可以分为标准类型和对象类型,进一步标准类型又可以分为原始类型和引用类型,而对象类型又可以分为内置对象类型普通对象类型自定义对象类型。
类型系统
类型转化表搬运自个人博客,原址JavaScript类型识别
javascript 类型系统可以分为标准类型和对象类型,进一步标准类型又可以分为原始类型和引用类型,而对象类型又可以分为内置对象类型、普通对象类型、自定义对象类型。
| Value | Boolean | Number | String |
|---|---|---|---|
| undefined | false | NaN | "undefined" |
| null | false | 0 | "null" |
| true | true | 1 | "true" |
| false | false | 0 | "false" |
| "" | false | 0 | "" |
| "123" | true | 123 | "123" |
| "1a" | true | NaN | "1a" |
| 0 | false | 0 | "0" |
| 1 | true | 1 | "1" |
| Infinity | true | Infinity | "Infinity" |
| NaN | false | NaN | "NaN" |
| {} | true | NaN | "[object Object]" |
typeof
instanceof
Object.prototype.toString
constructor
typeof可以识别标准类型(null除外)
不可识别具体的对象类型(Function除外)
例:
//1. 可以识别标准类型(`null`除外)
typeof(1);//"number"
typeof("");//"string"
typeof(undefined);//"undefined"
typeof(true);//"boolean"
typeof(null);//"object"
//2. 不可识别具体的对象类型(`Function`除外)
typeof([]);//"object"
typeof({});//"object"
typeof(function(){});//"function"
instanceof
instanceof左侧为查询变量,右侧为标识对象的类
能够判别内置对象类型
不能判别原始类型
能够判别自定义类型
例:
//1. 能够判别内置对象类型
[] instanceof Array;//true
/d/ instanceof RegExp;//true
//2. 不能判别原始类型
1 instanceof Number;//false
"xiaohong" instanceof String;//false
//3. 能够判别自定义类型
function Point(x, y) {
this.x = x;
this.y = y;
}
var c = new Point(2,3);
c instanceof Point;//true
Object.prototype.toString.call()方法
可以识别标准类型,及内置对象类型
不能识别自定义类型
例:
//1. 可以识别标准类型,及内置对象类型
Object.prototype.toString.call(21);//"[object Number]"
Object.prototype.toString.call([]);//"[object Array]"
Object.prototype.toString.call(/[A-Z]/);//"[object RegExp]"
//2. 不能识别自定义类型
function Point(x, y) {
this.x = x;
this.y = y;
}
var c = new Point(2,3);//c instanceof Point;//true
Object.prototype.toString.call(c);//"[object Object]"
为了方便使用,使用函数封装如下:
function typeProto(obj) {
return Object.prototype.toString.call(obj).slice(8,-1);
}
typeProto("guo");//"String"
typeProto({});//"Object"
constructor
constructor指向构造这个对象的构造函数本身..
可识别原始类型
可识别内置对象类型
可识别自定义类型
例:
//1. 可识别原始类型
"guo".constructor === String;//true
(1).constructor === Number;//true
true.constructor === Boolean;//true
({}).constructor === Object;//true
//2. 可识别内置对象类型
new Date().constructor === Date;//true
[].constructor === Array;//true
//3. 可识别自定义类型
function People(x, y) {
this.x = x;
this.y = y;
}
var c = new People(2,3);
c.constructor===People;//true
为了方便使用,使用函数封装如下:
function getConstructorName(obj) {
return obj && obj.constructor && obj.constructor.toString().match(/functions*([^(]*)/)[1];
}
getConstructorName(new Date());//"Date"
getConstructorName(null);//null
getConstructorName(12);//"Number"
类型判断对比表
其中红色的单元格表示该判断方式不支持的类型。
参考:
网易云课堂:面向对象软件开发实践之专业技能训练
网易云课堂前端微专业笔记
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/91532.html
摘要:基本数据类型中有种简单数据类型也称基本数据类型,,,和。不支持任何创建自定义类型的机制,而所有值都是上述中数据类型之一。 基本数据类型 ECMAScript中有5种简单数据类型(也称基本数据类型):Undefined,Null,Boolean,Number和String。还有一种复杂数据类型(引用型)Object。ECMAScript不支持任何创建自定义类型的机制,而所有值都是上述6中...
摘要:目录基本类型和引用类型检测数据类型的方法数据类型的转换通用库的初步创建与对象的深浅拷贝小结从前面三篇文章,我们已经大致了解的基本语法中的数据类型数值型字符串布尔型对象数组函数和这篇文章将深入探讨这几种数据类型在内存中的存储方式读取方式和拷贝 目录 1. 基本类型和引用类型 2. 检测数据类型的方法 3. 数据类型的转换 4. 通用库的初步创建与对象的深浅拷贝 5. 小结 从前面三篇文...
阅读 3986·2021-11-24 09:39
阅读 906·2019-08-30 14:22
阅读 3202·2019-08-30 13:13
阅读 2492·2019-08-29 17:06
阅读 3133·2019-08-29 16:22
阅读 1401·2019-08-29 10:58
阅读 2594·2019-08-26 13:47
阅读 1790·2019-08-26 11:39