javascript使用_用JavaScript投放

javascript使用

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中,我们具有以下原始类型:

and the object type:

和对象类型:

  • Object

    Object

(plus null and undefined, but there’s no point in casting from/to them)

(加上nullundefined ,但在它们之间进行强制转换没有任何意义)

For example you might want to convert:

例如,您可能要转换:

  • a number to a string

    字符串中的数字
  • a string to a number

    字符串到数字
  • a string to a boolean

    布尔值的字符串
  • a boolean to a string

    字符串的布尔值

…and so on.

…等等。

Here are the techniques you can use to convert from one type to another. I cover the most common cases.

这里是您可以用来将一种类型转换为另一种类型的技术。 我介绍了最常见的情况。

转换为字符串 (Converting to strings)

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()全局函数。

从数字到字符串的转换 (Casting from number to string)

Use the String global function, or the Number type toString() method:

使用String全局函数,或使用Number类型的toString()方法:

String(10) //"10"
(10).toString() //"10"

从布尔值转换为字符串 (Casting from boolean to string)

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"

从日期到字符串的转换 (Casting from date to string)

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)"

带弦的特殊情况 (Special cases with string)

String(null) //"null"
String(undefined) //"undefined"
String(NaN) //"NaN"

转换为数字 (Converting to numbers)

从字符串转换为数字 (Casting from string to number)

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") //1
Number("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 how to convert a string to a number in JavaScript. There are other ways to generate numbers from string including parseInt(), parseFloat(), Math.floor(), the unary + operator.

这是转换为数字的基础,但是我提供了有关如何在JavaScript中将字符串转换为数字的更多详细信息。 还有其他从字符串生成数字的方法,包括parseInt()parseFloat()Math.floor() ,一元+运算符。

从布尔值转换为数字 (Casting from boolean to number)

Just as we did for string, passing a boolean to Number() will return either 0 or 1:

就像我们对字符串所做的一样,将布尔值传递给Number()将返回0或1:

Number(true) //1
Number(false) //0

从日期到数字的转换 (Casting from date to number)

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() ,它将返回日期时间戳,这是您可以获得的最佳数字转换日期。

带数字的特殊情况 (Special cases with number)

Number(null) //0
Number(undefined) //NaN
Number(NaN) //NaN

转换为布尔值 (Converting to booleans)

Any value can be converted to boolean passing it to Boolean().

任何值都可以转换为布尔值,并将其传递给Boolean()

All values will resolve to true except:

除以下true外,所有值都将解析为true

Boolean(false) //false
Boolean(0) //false
Boolean(NaN) //false
Boolean("") //false
Boolean(null) //false
Boolean(undefined) //false

翻译自: https://flaviocopes.com/javascript-casting/

javascript使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值