博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript类型转换(广播)
阅读量:2504 次
发布时间:2019-05-11

本文共 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)

(加上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") //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() ,一元+运算符。

从布尔值转换为数字 (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) //1Number(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) //0Number(undefined) //NaNNumber(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) //falseBoolean(0) //falseBoolean(NaN) //falseBoolean("") //falseBoolean(null) //falseBoolean(undefined) //false

翻译自:

转载地址:http://ixqgb.baihongyu.com/

你可能感兴趣的文章
ubuntu开机自启动
查看>>
【WXS数据类型】Number
查看>>
canvas学习笔记 ---- 1
查看>>
纪念开博客的第一天_(:з」∠)_
查看>>
WechatHelper
查看>>
测试SDWebImage淡入淡出效果在UITableView中的重用显示问题
查看>>
[控件] ChangeColorLabel
查看>>
将位图导入为ArcGIS面要素
查看>>
前自增和后自增的区别
查看>>
Linux 基本操作
查看>>
ManagementClass("Win32_Share")之共享目录
查看>>
利用php制作对战后台程序
查看>>
【Leetcode】【Easy】Remove Linked List Elements
查看>>
django 反向解析
查看>>
快速排序一(划分)
查看>>
Oracle面试问题汇总
查看>>
Java程序员必知的8大排序
查看>>
数据库访问性能优化(二)
查看>>
Java-堆排序
查看>>
URI跳转方式地图导航的代码实践
查看>>