当前位置:网站首页>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] }
边栏推荐
- jeecg master-slave database read-write separation configuration "recommended collection"
- LeetCode_733_图像渲染
- Summary of the implementation method of string inversion "recommended collection"
- 数据库的范式(第一范式,第二范式,第三范式,BCNF范式)「建议收藏」
- 2.索引及调优篇【mysql高级】
- leetcode303 Weekly Match Replay
- Website vulnerability repair service provider's analysis of unauthorized vulnerability
- Matlab matrix basic operations (definition, operation)
- How does automated testing create business value?
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
猜你喜欢

外媒所言非虚,苹果降价或许是真的在清库存

C language "the third is" upgrade (mode selection + AI chess)

Premiere Pro 2022 for (pr 2022)v22.5.0

C语言”三子棋“升级版(模式选择+AI下棋)

C程序是如何跑起来的01 —— 普通可执行文件的构成

type of timer

WPF project - basic usage of controls entry, you must know XAML

Kubernetes principle analysis and practical application manual, too complete

【TypeScript】深入学习TypeScript类型操作

What is the difference between BI software in the domestic market?
随机推荐
How Redis handles concurrent access
Foreign media right, apple on May be true in inventory
使用 GraphiQL 可视化 GraphQL 架构
字符指针赋值[通俗易懂]
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)
Kubernetes常用命令
mysql black window ~ build database and build table
MySQL基础篇【单行函数】
做事软件开发-法的重要性所在以及合理结论的认识
2.索引及调优篇【mysql高级】
The principle of hough transform detection of straight lines (opencv hough straight line detection)
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
The use of button controls
LeetCode_733_图像渲染
数据表插入数据insert into
[TypeScript] In-depth study of TypeScript type operations
双边滤波加速「建议收藏」
Handling write conflicts under multi-master replication (4) - multi-master replication topology
leetcode303 Weekly Match Replay