var a = 32;
console.log(a.toString(2)); //返回字符串100000
console.log(a.toString(4)); //返回字符串200
console.log(a.toString(16)); //返回字符串20
console.log(a.toString(30)); //返回字符串12
console.log(a.toString(32)); //返回字符串10
console.log(123.toString()); //抛出语法错误 相当于打印 123.0toString()
console.log(1.23.toString()); //返回123
console.log((123).toString()); //返回123
console.log(123 .toString()); //返回123
var a = 123;
console.log(a.toString()); //返回123
1.undefined和null没有toString()方法
2.布尔型数据true和false返回对应的’true’和’false’
3.字符串类型原值返回
4.数值类型的情况较复杂
4.1、正浮点数及NaN、Infinity加引号返回
1.23.toString();//'1.23'
NaN.toString();//'NaN'
Infinity.toString();//'Infinity'
4.2、负浮点数或加’+'号的正浮点数直接跟上.toString(),相当于先运行toString()方法,再添加正负号,转换为数字
+1.23.toString();//1.23
typeof +1.23.toString();//'number'
-1.23.toString();//-1.23
typeof -1.23.toString();//'number'
4.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"
其他识别
除了类型识别之外,还可以进行其他识别,如识别arguments或DOM元素
(function(){
console.log(Object.prototype.toString.call(arguments));//[object Arguments]
})()
console.log(Object.prototype.toString.call(document));//[object HTMLDocument]
(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] }"
文章浏览阅读2k次。点击打开链接_mysql8安装 提示缺少framewark
文章浏览阅读461次。QToolBar 小部件是一个可移动的面板,由文本按钮、带有图标的按钮或其他小部件组成。它通常位于菜单栏下方的水平栏中,并且可以浮动的。_python qtoolbar
文章浏览阅读351次。本次美赛建模个人认为难度排名:A=D>B>C>E=F;上手简单程度:C>E=F>B>A=D。由于经历有限,本次我会主要针对 C 题进行助攻,包括但不限于:具体思路分析和数据挖掘,资料查找,数据可视化和模型建立,以及半成品论文等。关注wx:小何数模,获取本次比赛 C 题助攻资料!_24美赛c题
文章浏览阅读1.9k次。转载自http://blog.csdn.net/xiashaohua/article/details/53842414对于Audio在通话中的处理,根据不同的功能,有不同的调用路径, 1) 通话中 打开speaker 流程 enableSpeaker 对于在通话中打开扬声器,走图示1的调用路径, 在telephony部分,调用流程如下,_android audio invalidatestream
文章浏览阅读1.1w次,点赞6次,收藏23次。转:http://jingyan.baidu.com/article/4dc40848be2fd2c8d946f1a0.html本文教您:1,网站的域名和端口是什么含义2,网站的端口到底有哪些方法/步骤1通俗化解释什么是网站的端口,特别实用。-------------------------------_知道域名查看网址的端口
文章浏览阅读395次。Hey all, for a few of my college assignments I've found the need to check neighbouring cells in 2-dimensional arrays (grids). The solution I've used is a bit of a hack using exceptions, and I'm lookin..._用java编写一段程序,检查一个二维数组是否对称,(即对所有和都有a[i][j]=a[j][i])
文章浏览阅读8k次,点赞2次,收藏4次。有时候我们完全按照ui 布局写的代码 看到的效果却和ui的效果有一定的差距有没有很苦恼的效果,最近帮同事改bug 发现了这个问题,很多地方都是他私自调节的这个问题不可有啊,还是要按照ui的尺寸来,这种情况一般出现在字体的sp 比较大的情况先看下的效果图第一个和第三个明显文字的上下内间距很大,2和4 使用的属性为:android:includeFontPadding="false"代码如下 ,这样设置以后可以取消一定的间距,但是不能完全消除内边距,这样设置以后基本都达到ui的要.._android 去除textview空白
文章浏览阅读890次,点赞13次,收藏20次。在生产环境中,为了实现ThingsBoard PE专业版集群的高效部署,我们需要对CentOS操作系统、Linux内核、Docker容器技术、Rancher容器管理平台、Kubernetes容器编排系统以及ThingsBoard的版本进行周密的规划。这一规划过程是为了确保各组件间的兼容性、稳定性以及性能优化,从而满足实际生产的需求。_rancher生产环境部署哪个版本
文章浏览阅读1w次,点赞8次,收藏24次。我们之前测试了OpenVINO在x86系统上的模型推理加速,本次我们在win10系统下基于cuda+GPU显卡下用tensorRT做engine模型转换,因为显卡太低端最后还是没能生成engine模型,但是过程可以借鉴。_engine/trt模型
文章浏览阅读959次。SIP SDP Profile-level-id解析基于SIP协议的VOIP通信,该字段通常位于视频协商sdp报文中,如:[html] view plain copy print?在CODE上查看代码片派生到我的代码片video 23456 RTP/AVP 121rtpmap:121 H264/90000fmtp: 121 profile-level-id=42801E; packetization-mode=142801E分三部分0x42 660x80 1000 0000 (二进制)0x_sip profileid
文章浏览阅读446次。前言前段时间,学习到了Flutter动画,正愁不知道写个项目练习巩固,突然有一天产品在群里发了一个链接【ios中的动画标签】(下面有例图),我心里直呼"好家伙",要是产品都要求做成这样,产品经理和程序员又又又又又又得打起来! 还好只是让我们参考,刚好可以拿来练习。GitHub地址:https://github.com/longer96/flutter-demo我们每天都会看到底部导航菜单,它们在应用程序内引导用户,允许他们在不同的tag之间快速切换。但是谁说切换标签就应该很无聊?让我们一起探索标签_flutter 多级menu
文章浏览阅读1.2k次,点赞3次,收藏3次。话不多说直接来个正文~————————————下面是正文——————————————如果此时需要你来编写一个函数需求是传进两个参数,对传进来参数进行数值交换,大多数人可能会写出以下的代码:#include <iostream>using namespace std; void swap(int x,int y){ int temp; temp = x..._swap是值传递吗