本文共 3794 字,大约阅读时间需要 12 分钟。
Even if JavaScript is a loosely typed language, you might have the need to convert a value from a type to another.
即使JavaScript是一种松散类型的语言,您也可能需要将一个值从一种类型转换为另一种。
In JavaScript we have those primitive types:
在JavaScript中,我们具有以下原始类型:
Boolean
Boolean
and the object type:
和对象类型:
(plus null
and undefined
, but there’s no point in casting from/to them)
(加上null
和undefined
,但在它们之间进行强制转换没有任何意义)
For example, you might want to convert:
例如,您可能要转换:
…and so on.
…等等。
Here are the techniques you can use to convert from one type to another. I cover the most common cases.
这里是您可以用来将一种类型转换为另一种类型的技术。 我介绍了最常见的情况。
In general converting from anything to a string is usually a matter of calling the toString()
method on any value, and JavaScript will create a string value corresponding to that type. Or you can pass any value to the String()
global function.
通常,从任何内容转换为字符串通常是对任何值调用toString()
方法的问题,JavaScript将创建与该类型相对应的字符串值。 或者,您可以将任何值传递给String()
全局函数。
Use the String global function, or the Number type toString()
method:
使用String全局函数,或使用Number类型的toString()
方法:
String(10) //"10"(10).toString() //"10"
Use the String global function, or the Boolean type toString()
method:
使用String全局函数或布尔类型的toString()
方法:
String(true) //"true"true.toString() //"true"String(false) //"false"false.toString() //"false"
Use the String global function, or the Date type toString()
method:
使用String全局函数,或使用Date类型的toString()
方法:
String(new Date('2019-01-22'))//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"(new Date('2019-01-22')).toString()//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"
String(null) //"null"String(undefined) //"undefined"String(NaN) //"NaN"
We can do this by using the Number()
global function, which is sort of a constructor. We can pass it a string, and JavaScript will figure out how to convert it to a number:
我们可以通过使用Number()
全局函数(它是一种构造函数Number()
来实现此目的。 我们可以将其传递给字符串,JavaScript会找出如何将其转换为数字:
Number("1") //1Number("0") //0
Strings are trimmed before being converted to numbers:
在将字符串转换为数字之前对其进行修剪:
Number(" 1 ") //1
passing an empty string defaults to 0:
传递空字符串默认为0:
Number("") //0
and to have work with decimals you use a dot:
要使用小数,可以使用点:
Number("12.2")
If a string contains invalid characters, it will generate a NaN
.
如果字符串包含无效字符,则将生成NaN
。
This are the basics of converting to numbers, but I give a lot more details in . There are other ways to generate numbers from string including parseInt()
, parseFloat()
, Math.floor()
, the unary +
operator.
这是转换为数字的基础,但是我提供了有关转换为数字的更多详细信息。 还有其他从字符串生成数字的方法,包括parseInt()
, parseFloat()
, Math.floor()
,一元+
运算符。
Just as we did for string, passing a boolean to Number()
will return either 0 or 1:
就像我们对字符串所做的一样,将布尔值传递给Number()
将返回0或1:
Number(true) //1Number(false) //0
If you pass a Date object to Number()
, it will return the date timestamp, which is the best date to number conversion you can get.
如果将Date对象传递给Number()
,它将返回日期时间戳,这是您可以获得的最佳数字转换日期。
Number(null) //0Number(undefined) //NaNNumber(NaN) //NaN
Any value can be converted to boolean passing it to Boolean()
.
任何值都可以转换为布尔值,并将其传递给Boolean()
。
All values will resolve to true
except:
除以下true
外,所有值都将解析为true
:
Boolean(false) //falseBoolean(0) //falseBoolean(NaN) //falseBoolean("") //falseBoolean(null) //falseBoolean(undefined) //false
翻译自:
转载地址:http://ixqgb.baihongyu.com/