10012---JavaScript--类型转换

来源:互联网 发布:手机淘宝店怎样发货 编辑:程序博客网 时间:2024/05/29 13:32


Number() 转换为数字, String() 转换为字符串, Boolean() 转化为布尔值。


JavaScript 数据类型

在 JavaScript 中有 5 中不同的数据类型:

  • string
  • number
  • boolean
  • object
  • function

    【注】函数也是一种数据类型

3 种对象类型:

  • Object
  • Date
  • Array

2 个不包含任何值的数据类型:

  • null
  • undefined

typeof 操作符

你可以使用 typeof 操作符来查看 JavaScript 变量的数据类型。

typeof "John"                 // 返回 string  typeof 3.14                   // 返回 number typeof NaN                    // 返回 numbertypeof false                  // 返回 booleantypeof [1,2,3,4]              // 返回 object typeof {name:'John', age:34}  // 返回 objecttypeof new Date()             // 返回 objecttypeof function () {}         // 返回 function typeof myCar                  // 返回 undefined (if myCar is not declared) typeof null                   // 返回 object 

请注意:

  • NaN 的数据类型是 number
  • 数组(Array)的数据类型是 object
  • 日期(Date)的数据类型为 object
  • null 的数据类型是 object
  • 未定义变量的数据类型为 undefined

constructor 属性

constructor 属性返回所有 JavaScript 变量的构造函数。

  John".constructor                 // 返回函数 String()  { [native code] } (3.14).constructor                 // 返回函数 Number()  { [native code] } false.constructor                  // 返回函数 Boolean() { [native code] } [1,2,3,4].constructor              // 返回函数 Array()   { [native code] } {name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] } new Date().constructor             // 返回函数 Date()    { [native code] } function () {}.constructor         // 返回函数 Function(){ [native code] } 

你可以使用 constructor 属性来查看是对象是否为数组 (包含字符串 "Array"):

你可以使用 constructor 属性来查看是对象是否为日期 (包含字符串 "Date"):

function isArray(myArray) {     return myArray.constructor.toString().indexOf("Array") > -1; }function isDate(myDate) {     return myDate.constructor.toString().indexOf("Date") > -1; } 

JavaScript 类型转换

JavaScript 变量可以转换为新变量或其他数据类型:

  • 通过使用 JavaScript 函数
  • 通过 JavaScript 自身自动转换

将数字转换为字符串

全局方法 String() 可以将数字转换为字符串。

该方法可用于任何类型的数字,字母,变量,表达式:

 String(x)         // 将变量 x 转换为字符串并返回 String(123)       // 将数字 123 转换为字符串并返回 String(100 + 23)  // 将数字表达式转换为字符串并返回
Number 方法 toString() 也是有同样的效果。
x.toString()(123).toString()(100 + 23).toString()

在 Number 方法 章节中,你可以找到更多数字转换为字符串的方法:

方法描述toExponential()把对象的值转换为指数计数法。toFixed()把数字转换为字符串,结果的小数点后有指定位数的数字。toPrecision()把数字格式化为指定的长度。

将布尔值转换为字符串

全局方法 String() 可以将布尔值转换为字符串。

 String(false)        // 返回 "false" String(true)         // 返回 "true"

Boolean 方法 toString() 也有相同的效果。
 false.toString()     // 返回 "false" true.toString()      // 返回 "true" 

将日期转换为字符串

全局方法 String() 可以将日期转换为字符串。

String(Date())      // 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)

Date 方法 toString() 也有相同的效果。

在 Date 方法 章节中,你可以查看更多关于日期转换为字符串的函数:

方法描述getDate()从 Date 对象返回一个月中的某一天 (1 ~ 31)。getDay()从 Date 对象返回一周中的某一天 (0 ~ 6)。getFullYear()从 Date 对象以四位数字返回年份。getHours()返回 Date 对象的小时 (0 ~ 23)。getMilliseconds()返回 Date 对象的毫秒(0 ~ 999)。getMinutes()返回 Date 对象的分钟 (0 ~ 59)。getMonth()从 Date 对象返回月份 (0 ~ 11)。getSeconds()返回 Date 对象的秒数 (0 ~ 59)。getTime()返回 1970 年 1 月 1 日至今的毫秒数。

【注】Date() 返回的是字符串;new Date()返回的是日期对象;要用日期时应该用 new Date()。

将字符串转换为数字

全局方法 Number() 可以将字符串转换为数字。

字符串包含数字(如 "3.14") 转换为数字 (如 3.14).

空字符串转换为 0。

其他的字符串会转换为 NaN (不是个数字)。

Number("3.14")    // 返回 3.14 Number(" ")       // 返回 0  Number("")        // 返回 0 Number("99 88")   // 返回 NaN 



0 0
原创粉丝点击