当前位置:网站首页>JS four formulas for judging data types
JS four formulas for judging data types
2022-07-29 00:02:00 【Fairy loves fish】
1.Js Determine the type of data 4 Kind of ⽅ type
Js data type es5 6 Type of data :string、number、boolean、object(Data、function、Array)、null、undefined
Es6 Added in ⼀ Type of data :symbol: Express independence ⼀⽆⼆ Value , most ⼤ Of ⽤ Law is ⽤ To define the object ⼀ Property name ,⽆ On inequality at all times
var s1 = Symbol("symbol");
1.typeof
- For basic types , except null outside , Can return the correct result .
- For citation ⽤ type , except function outside ,⼀ Law return object type .
- about null , return object type .
- about function return function type .
Typeof NaN = number
Typeof null = object
Typeof object = function Object/Array/String/Number/Boolean/Date/RegExp All constructors
Typeof {} = object
typeof console.log = ‘function’ The function itself
typeof console.log() = ‘undefined’ shipment ⾏ function , Default return undefined
2.instanceof
test A Whether it is B Example ,⽐ a A._proto Whether and B.prototype equal
,instanceof Can only ⽤ To determine whether the two objects belong to the instance relationship , ⽽ Can't judge ⼀ Which type of object instances belong to .
[] instanceof Array
{} instanceof Object
instanceof (A,B) = {
var L = A.__proto__;
var R = B.prototype;
if(L === R) {
// A The internal properties of __proto__ Point to B Prototype object
return true;
}
return false;
}
3.constructor
- null and undefined yes ⽆ Effective object , So there won't be constructor There is , These two types of data need to be passed through other ⽅ To judge .
‘’.constructor === ‘String’
New Number(1).constructor === ‘Number’
New Function().constructor === ‘function’
4.toString
Object The prototype of the ⽅ Law , Returns the... Of the current object [[Class]]
Object.prototype.toString.call(‘’) // [[Object String]]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window Global object global Introduction of ⽤
边栏推荐
- Zibo station construction guide (aigler)
- Leetcode59. Spiral matrix II
- 实时数仓:网易严选基于Flink的实时数仓实践
- 基于 FPGA 实现数字时钟详细原理讲解及验证结果
- Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan
- Oracle创建表空间和用户
- 迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
- Powercli batch add esxi to vCenter
- Android studio连接MySQL并完成简单的登录注册功能
- 通配符 SSL/TLS 证书
猜你喜欢
随机推荐
Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (2. Compatibility of kingbasees v8.3 and v8.6)
基因组 DNA 分离丨Worthington核糖核酸酶A
PHP poster QR code synthesis
EN 1935建筑五金.单轴铰链—CE认证
【微服务】Nacos集群搭建以及加载文件配置
Worthington核糖核酸酶B历史和化学性质说明
Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan
Android studio连接MySQL并完成简单的登录注册功能
[data mining engineer - written examination] Dahua shares in 2022
DoIP测试开发实践
CANoe应用案例之DoIP通信
Machine learning problem notes
【C】替换空格,宏实现整数的二进制奇偶位交换
Arm-a53 data "recommended collection"
【TA-霜狼_may-《百人计划》】美术2.2 模型基础
【MySQL 8】Generated Invisible Primary Keys(GIPK)
DevOps在物联网解决方案中的应用
JS高级 之 ES6~ES13 新特性
Powercli batch add esxi to vCenter
1-8 basic use of props








