当前位置:网站首页>js的toString方法
js的toString方法
2022-07-31 15:55:00 【youhebuke225】
专栏目录请点击
toString()参数为字符串的转换基数 点击
数据类型
undefined与null
一般undefined与null,没有toString()方法,但是从ES6之后规定:null返回[object Null],undefined返回[object Undefined],当然,我们也可以用来检测一些其他的类型 点击
数组
数组调用toString,返回一个字符串,他是数组中的元素 点击
// 数组
const arr = [1,2,3]
console.log(arr.toString()) // 1,2,3
布尔值
布尔型数据true和false返回对应的’true’和’false’
字符串
字符串类型原值返回
数值
- 正浮点数及NaN、Infinity加引号返回,如果有
-号,那么他会先执行toString方法,然后执行-号的运算
1.23.toString();//'1.23'
typeof 1.23.toString(); //string
NaN.toString();//'NaN'
Infinity.toString();//'Infinity'
-1.23.toString(); // -1.23
typeof -1.23.toString() // number
- 整数调用,如果整数连续调用,就必须加上括号,不然的话
.会被当做小数点而报错
// 整数
0.toString() // 错误的写法
(0).toString() // 0
当然,我们在写的时候,编辑器也会给我们报错
对象
会得到[object Object]的字符串
const obj = {
}
console.log(obj.toString()) // [object Object]
类型判断
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({
name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){
}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){
};
console.log(Object.prototype.toString.call(new Person));//[object Object]
类型识别函数
所以我们可以定义一个函数来进行类型的识别
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
console.log(type("jerry"));//"string"
console.log(type(12));//"number"
console.log(type(true));//"boolean"
console.log(type(undefined));//"undefined"
console.log(type(null));//"null"
console.log(type({
name: "jerry"}));//"object"
console.log(type(function(){
}));//"function"
console.log(type([]));//"array"
console.log(type(new Date));//"date"
console.log(type(/\d/));//"regexp"
function Person(){
};
console.log(type(new Person));//"object"
其他的识别功能
arguments与DOM
(function(){
console.log(Object.prototype.toString.call(arguments));//[object Arguments]
})()
console.log(Object.prototype.toString.call(document));//[object HTMLDocument]
自定义函数与内置函数
- 自定义函数会得到函数的源码
- 内置函数会有
native code的字符串
function fn(){
console.log("hello world");
}
console.log(fn.toString())
console.log(Function.toString())
function fn(){
console.log("hello world");
}
function Function() { [native code] }
边栏推荐
- JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」
- gerrit中如何切换远程服务器
- t-sne 数据可视化网络中的部分参数+
- Oracle动态注册非1521端口
- MySQL常用语句整理
- jeecg master-slave database read-write separation configuration "recommended collection"
- Vb how to connect mysql_vb how to connect to the database collection "advice"
- Baidu cloud web speed playback (is there any website available)
- C language "the third is" upgrade (mode selection + AI chess)
- T - sne + data visualization parts of the network parameters
猜你喜欢
![[MySQL] Mysql paradigm and the role of foreign keys](/img/9d/a4295de26683d7bca2b8e9d14f754b.png)
[MySQL] Mysql paradigm and the role of foreign keys

Browser's built-in color picker

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency

MySQL的相关问题

How to switch remote server in gerrit

Premiere Pro 2022 for (pr 2022)v22.5.0

radiobutton的使用

i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)

基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀

EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
随机推荐
Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
border控件的使用
Baidu cloud web speed playback (is there any website available)
Graham's Scan method for solving convex hull problems
Snake Project (Simple)
MySQL多表联合查询
Browser's built-in color picker
基于ABP实现DDD
C程序是如何跑起来的01 —— 普通可执行文件的构成
json到底是什么(c# json)
使用 GraphiQL 可视化 GraphQL 架构
网站漏洞修复服务商关于越权漏洞分析
[MySQL] Mysql paradigm and the role of foreign keys
全新宝马3系上市,安全、舒适一个不落
百度网盘网页版加速播放(有可用的网站吗)
mysql黑窗口~建库建表
LeetCode_733_图像渲染
贪吃蛇项目(简单)
Implementing DDD based on ABP
Replication Latency Case (1) - Eventual Consistency