Detect data type【BFE.dev】

本文介绍了如何使用`Object.prototype.toString.call()`方法在BFE.dev中检测数据类型,包括处理FileReader特殊情况,并提到可以使用正则表达式或字符串连接来解析返回的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The 20th question on BFE.dev, link is here Detect data type.
To detect data type, we should mainly use Object.prototype.toString.call() this api. This api will return a stuff like [Object xxx](here is something that represents the type of value), then we can get the string stuff through some ways.
We can use regular expression, or we can also use simple string concatenation to settle the problem.

function detectType(data) {
    if(data instanceof FileReader) return 'object';
    return Object.prototype.toString.call(data).slice(1, -1).split(' ')[1].toLowerCase();
}

We can use slice and two parameters of it are 1 and -1 respectively, in this way we can extract the content in square brackets. Then we can use split(’ ')(we put a blank space here) so we can get an array within two elements, and the second one is what we want.
But for this question, there is a special situation, when we encounter a FileReader, the type of it should be object but not filereader, so we should handle this scenario, so we give a if condition, check if the object is the instance of FileReader, if so, simply return ‘object’.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值