千家信息网

toString()方法

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,【1】undefined和null没有toString()方法undefined.toString();//错误null.toString();//错误【2】布尔型数据true和false返回对应的'
千家信息网最后更新 2026年10月31日toString()方法

【1】undefined和null没有toString()方法

undefined.toString();//错误null.toString();//错误

【2】布尔型数据true和false返回对应的'true'和'false'

true.toString();//'true'false.toString();//'false'Boolean.toString();//"function Boolean() { [native code] }"

【3】字符串类型原值返回

'1'.toString();//'1'''.toString();//'''abc'.toString();//'abc'String.toString();//"function String() { [native code] }"

【4】数值类型的情况较复杂

Number.toString();//"function Number() { [native code] }"

  1、正浮点数及NaN、Infinity、-Infinity加引号返回

1.23.toString();//'1.23'NaN.toString();//'NaN'Infinity.toString();//'Infinity'-Infinity.toString();//'-Infinity'

  2、负浮点数或加'+'号的正浮点数直接跟上.toString(),toString()无效并返回原数值

+1.23.toString();//1.23typeof +1.23.toString();//'number'-1.23.toString();//-1.23typeof -1.23.toString();//'number'

  3、整数直接跟上.toString()形式,会报错,提示无效标记,因为整数后的点会被识别为小数点

0.toString();//Uncaught SyntaxError: Invalid or unexpected token

  因此,为了避免以上无效及报错的情况,数字在使用toString()方法时,加括号可解决

(0).toString();//'0'(-0).toString();//'0'(+1.2).toString();//'1.2'(-1.2).toString();//'-1.2'(NaN).toString();//'NaN'

  此外,数字类型的toString()方法可以接收表示转换基数(radix)的可选参数,如果不指定此参数,转换规则将是基于十进制。同样,也可以将数字转换为其他进制数(范围在2-36)

var n = 17;n.toString();//'17'n.toString(2);//'10001'n.toString(8);//'21'n.toString(10);//'17'n.toString(12);//'15'n.toString(16);//'11'

【5】对象Object类型及自定义对象类型加括号返回[object Object]

{}.toString();//报错,Unexpected token .({}).toString();//[object Object]({a:123}).toString();//[object Object]Object.toString();//"function Object() { [native code] }"
function Person(){    this.name = 'test';}var person1 = new Person();person1.toString();//"[object Object]"

类型识别

  常常使用Object.prototype.toString()来进行类型识别,返回代表该对象的[object 数据类型]字符串表示

  [注意]Object.prototype.toString()可以识别标准类型及内置对象类型,但不能识别自定义类型

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"

【6】函数Function类型返回函数代码

  当我们对一个自定义函数调用toString()方法时,可以得到该函数的源代码;如果对内置函数使用toString()方法时,会得到一个'[native code]'字符串。因此,可以使用toString()方法来区分自定义函数和内置函数

function test(){    alert(1);//test}test.toString();/*"function test(){                    alert(1);//test                  }"*/Function.toString();//"function Function() { [native code] }"

【7】数组Array类型返回由数组中每个值的字符串形式拼接而成的一个以逗号分隔的字符串

[].toString();//''[1].toString();//'1'[1,2,3,4].toString();//'1,2,3,4'Array.toString();//"function Array() { [native code] }"

【8】时间Date类型返回表示当前时区的时间的字符串表示

(new Date()).toString();//"Sun Jun 05 2016 10:04:53 GMT+0800 (中国标准时间)"Date.toString();//"function Date() { [native code] }"

【9】正则表达式RegExp类型返回正则表达式字面量的字符串表示

/ab/i.toString();//'/ab/i'/mom( and dad( and baby)?)?/gi.toString();//'mom( and dad( and baby)?)?/gi'RegExp.toString();//"function RegExp() { [native code] }"

【10】错误Error类型

Error.toString();//"function Error() { [native code] }"RangeError.toString();//"function RangeError() { [native code] }"ReferenceError.toString();//"function ReferenceError() { [native code] }"SyntaxError.toString();//"function SyntaxError() { [native code] }"TypeError.toString();//"function TypeError() { [native code] }"URIError.toString();//"function URIError() { [native code] }"


类型 函数 字符 字符串 方法 对象 数字 点数 错误 参数 形式 情况 括号 数值 数据 数组 整数 时间 标准 正则 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 南京欣网互联网络科技有限公司 互联网科技公司起名 互联网科技工作委员会 西安纸贵互联网科技有限公司 中国互联网科技工作委员会毛军 互联网最新的科技 互联网中国科技 港股互联网科技股 高科技与互联网 致聘互联网科技 天下捷融互联网科技 人人互联网科技是培训 港股科技和互联网 长沙茂晟互联网科技有限公司 济南有互联网科技公司吗 信承诺互联网科技有限公司 全球科技与互联网公司市值变化 银川新华互联网科技学校招聘 广州全民伙伴互联网科技有限公司 颐高云石互联网科技 互联网科技网 北京禾田雨橡互联网科技 苏州大垚互联网科技 新兴科技互联网创新大赛颁奖典礼 互联网科技人工智能实验室 深圳市罗太互联网科技有限公司 广州富喜互联网络科技 江苏互联网科技小贷 吉林省码上互联网科技电话 互联网科技创业计划的风险分析
0