当前位置:网站首页>js基础 判断数据类型
js基础 判断数据类型
2022-07-30 05:41:00 【勿扰丶】
数据类型检测
typeof
typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 7 种:number、boolean、symbol、string、object、undefined、function 等。
typeof'';// string 有效
typeof1;// number 有效
typeofSymbol();// symbol 有效
typeoftrue;//boolean 有效
typeofundefined;//undefined 有效
typeofnull;//object 无效
typeof[] ;//object 无效
typeofnewFunction();// function 有效
typeofnewDate();//object 无效
typeofnewRegExp();//object 无效
instanceof
instanceof 是用来判断 A 是否为 B 的实例,表达式为:A instanceof B,如果 A 是 B 的实例,则返回 true,否则返回 false。 在这里需要特别注意的是:instanceof 检测的是原型,instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型
constructor 检测构造函数
利用原型对象上的 constructor 引用了自身,当 F 作为构造函数来创建对象时,原型上的 constructor 就被遗传到了新创建的对象上, 从原型链角度讲,构造函数 F 就是新对象的类型。这样做的意义是,让新对象在诞生以后,就具有可追溯的数据类型
1.null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
2.函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Object
3.为什么变成了 Object?
因为 prototype 被重新赋值的是一个 { }, { } 是 new Object() 的字面量,因此 new Object() 会将 Object 原型上的 constructor 传递给 { },也就是 Object 本身。
因此,为了规范开发,在重写对象原型时一般都需要重新给 constructor 赋值,以保证对象实例的类型不被篡改。
console.log("".constructor===String) // true
console.log(new Number(1).constructor===Number) // true
console.log(true.constructor===boolean) // true
console.log(new Function().constructor===Function) // true
console.log(new Date().constructor===Date) // true
console.log(new Error().constructor===Error) // true
console.log([].constructor===Array) // true
console.log(document.constructor===HTMLDocument) // true
console.log(window.constructor===window) // true
- Object.prototype.toString.call 检测数据类型 (推荐)
toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。
对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
Object.prototype.toString.call('') ; // [object String]
Object.prototype.toString.call(1) ; // [object Number]
Object.prototype.toString.call(true) ;// [object Boolean]
Object.prototype.toString.call(Symbol());//[object Symbol]
Object.prototype.toString.call(undefined) ;// [object Undefined]
Object.prototype.toString.call(null) ;// [object Null]
Object.prototype.toString.call(newFunction()) ;// [object Function]
Object.prototype.toString.call(newDate()) ;// [object Date]
Object.prototype.toString.call([]) ;// [object Array]
Object.prototype.toString.call(newRegExp()) ;// [object RegExp]
Object.prototype.toString.call(newError()) ;// [object Error]
Object.prototype.toString.call(document) ;// [object HTMLDocument]
Object.prototype.toString.call(window) ;//[object global] window 是全局对象 global 的引用
typeof [ value ] 返回当前值得数据类型 “数据类型”
返回结果都是字符串
局限性:typeof null => "object"
不可以细分对象类型,数组对象和普通对象都为 “object”
typeof 默认认为前几位为0的为object ,null 存贮为000 ;
typeof 12 //"number"
typeof null // "object" // 所有的值在内存中都是以二进制存贮的
typeof "number" // "string"
typeof undefined // "undefined"
typeof true // "boolean"
typeof Symbol('1'); // "symbol"
typeof BigInt('1') // "bigint"
typeof {
} // "object"
typeof [] //"object"
typeof function(){
} // "function"
// 例子
let a = typeof typeof typeof [12,14]
console.log(a) // "string"
let b = parseFloat("left:'100px'");
if(b===100){
console.log(100);
}else if(b===NaN){
console.log(nan)
}else if(typeof b === "number"){
console.log("number")
}else {
console.log("Invalid number")
}// number
// parseInt 处理的值是字符串,从字符串的左侧开始查找有效数字字符,
// 遇到非有效字符停止查找,如果处理的值不是字符串,需要先转化为字符串,在进行查找
// Number 直接用浏览器最底层的数据类型测试机制来完成
parseInt("") // NaN;
Number("") // 0;
isNaN("") // false false 代表有效数字
parseInt(null) ; // parseInt("null") NaN
Number("21px") // NaN
parseInt('1.2px') + parseFloat('1px') + typeof parseInt(null) // 1 + 1 + typeof NaN => 2+"number" =>"2number"
typeof !parseInt(null) + !isNaN(null) // "booleantrue"
10 + undefined // NaN
NaN+[] //NaN
"123dadsfa" + {
} // "123dadsfa[object,'object]"
//对象转换字符串先调用valueof 在调用toString
边栏推荐
- Qt在QTableWidget、View等表格中添加右击菜单
- 839. Simulated heap
- 秒杀项目的总结及面试常见问题
- mysql time field is set to current time by default
- cross_val_score的用法
- enumerate() 函数
- String类型字符串获取第一次或者最后一次出现的下标
- Navicat cannot connect to mysql super detailed processing method
- Frobenius norm(Frobenius 范数)
- [GStreamer] The name of the plugin should match GST_PLUGIN_DEFINE
猜你喜欢

net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.

torch.load()

‘kaggle视频游戏销售数据的可视化和分析‘项目实现

操作系统面试整理

EOF的用法——while(scanf(“%d“,&num)!=EOF)

“tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“

5.5线程池

【文献阅读】Age Progress/Regression by Conditional Adversarial Autoencoder 基于条件对抗自编码器(CAAE)的老化/去龄化方案

【C语言】三子棋(井字棋)的实现

面试前需要巩固的算法知识点(自用,更新中)
随机推荐
Frobenius norm(Frobenius 范数)
871.最低加油次数(动态规划)
P3 元宝第六单元笔记
秒杀项目的总结及面试常见问题
人生的第一篇博客(初识代码)
Sql操作
934.最短的桥(广度优先搜索)
argparse —— 命令行选项、参数和子命令解析器
多进程实现并发服务器
flask的笔记
MySQL Soul 16 Questions, how many questions can you last?
let到底会不会造成变量提升
C语言自定义类型一网打尽(结构体、位段/位域、枚举、联合体)
条件变量解决生产者消费者问题
运算符和交互基础
【文献阅读】Age Progress/Regression by Conditional Adversarial Autoencoder 基于条件对抗自编码器(CAAE)的老化/去龄化方案
51.N皇后(回溯法)
create-nuxt-app创建出来的项目没有server
Introduction to Oracle Patch System and Opatch Tool
海量号码需要保存,如何才能尽可能少地占用内存?