Object.prototype.toString.call(r).slice(8, -1)
时间: 2024-05-28 10:09:28 浏览: 9
这是一个获取任意变量类型的常用方法,具体说来,它返回变量r的类型。例如,如果r是一个字符串,它将返回"String"。如果r是一个数组,则返回"Array"。如果r是一个对象,则返回"Object"等等。这个方法通过调用 Object.prototype.toString 方法并传递 this 字段来实现。
相关问题
利用Object.prototype.toString.call(obj)判断数据类型
可以的,使用这个方法可以判断出一个 JavaScript 变量的数据类型。这个方法返回一个字符串,包含了变量的数据类型信息。例如,对于一个字符串变量,它返回的字符串是"[object String]"。可以通过判断这个字符串中是否包含某个关键字来判断变量的数据类型。下面是一个示例代码:
```
function getType(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
}
console.log(getType("hello")); // "String"
console.log(getType(123)); // "Number"
console.log(getType(true)); // "Boolean"
console.log(getType(null)); // "Null"
console.log(getType(undefined));// "Undefined"
console.log(getType({})); // "Object"
console.log(getType([])); // "Array"
console.log(getType(function(){})); // "Function"
```
Function fn(){console.log(“test”);} Object.prototype.toString.call(fn);//”[object Function]”
引用\[1\]中提到,当我们使用Object.prototype.toString.call()方法来判断一个对象的类型时,对于函数类型,会返回"\[object Function\]"。所以对于给定的代码,Function fn(){console.log("test");}的类型是函数,返回的结果是"\[object Function\]"。
#### 引用[.reference_title]
- *1* [js中的Object.prototype.toString.call()方法](https://blog.csdn.net/weixin_46872121/article/details/110178415)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Object.prototype.toString.call()和Array.prototype.slice.call()的原理](https://blog.csdn.net/m0_60360320/article/details/119303896)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Object.prototype.toString.call()方法使用](https://blog.csdn.net/weixin_40967353/article/details/107785840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)