亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript 類(lèi)型轉(zhuǎn)換

Number() 轉(zhuǎn)換為數(shù)字, String() 轉(zhuǎn)換為字串, Boolean() 轉(zhuǎn)換為布林值。


JavaScript 資料型別

#在JavaScript 中有5 個(gè)不同的資料型別:

  • string

  • number

  • #boolean

  • object

  • ##function

3 種物件類(lèi)型:

    ##Object
  • Date
  • Array

#2 個(gè)不包含任何值的資料型別:

null


undefined

  • typeof 運(yùn)算子
  • 你可以使用?typeof?運(yùn)算子來(lái)檢視JavaScript 變數(shù)的資料型態(tài)。

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
    </head>
    <body>
    <p> typeof 操作符返回變量、對(duì)象、函數(shù)、表達(dá)式的類(lèi)型。</p>
    <p id="demo"></p>
    <script>
    document.getElementById("demo").innerHTML = 
        typeof "john" + "<br>" +
        typeof 3.14 + "<br>" +
        typeof NaN + "<br>" +
        typeof false + "<br>" +
        typeof [1,2,3,4] + "<br>" +
        typeof {name:'john', age:34} + "<br>" +
        typeof new Date() + "<br>" +
        typeof function () {} + "<br>" +
        typeof myCar + "<br>" +
        typeof null;
    </script>
    </body>
    </html>

    執(zhí)行程式嘗試
  • 請(qǐng)注意:

  • #NaN 的資料型別是number

##陣列(Array)的資料型別是object


日期(Date)的資料型別為objectnull 的資料型別是object

未定義變數(shù)的資料型別為undefined


如果物件是JavaScript Array 或JavaScript Date ,我們就無(wú)法透過(guò)?typeof?來(lái)判斷他們的類(lèi)型,因?yàn)槎际腔貍鱋bject。

constructor 屬性


#constructor?屬性傳回所有 JavaScript 變數(shù)的建構(gòu)子。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p> constructor 屬性返回變量或?qū)ο蟮臉?gòu)造函數(shù)。</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 
    "john".constructor + "<br>" +
    (3.14).constructor + "<br>" +
    false.constructor + "<br>" +
    [1,2,3,4].constructor + "<br>" +
    {name:'john', age:34}.constructor + "<br>" +
    new Date().constructor + "<br>" +
    function () {}.constructor;
</script>
</body>
</html>

執(zhí)行程式嘗試

你可以使用constructor 屬性來(lái)檢視是物件是否為陣列(包含字串"Array"):

#實(shí)例


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>判斷是否為數(shù)組。</p>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = isArray(fruits);
function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
}
</script>
</body>
</html>
執(zhí)行程式嘗試一下#你可以使用constructor 屬性來(lái)查看是物件是否為日期(包含字串"Date") :

    實(shí)例
  • <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
    </head>
    <body>
    <p>判斷是否為日期。</p>
    <p id="demo"></p>
    <script>
    var myDate = new Date();
    document.getElementById("demo").innerHTML = isDate(myDate);
    function isDate(myDate) {
        return myDate.constructor.toString().indexOf("Date") > -1;
    }
    </script>
    </body>
    </html>
  • 執(zhí)行程式嘗試
JavaScript 類(lèi)型轉(zhuǎn)換

JavaScript 變數(shù)可以轉(zhuǎn)換為新變數(shù)或其他資料型別:

#透過(guò)使用JavaScript 函數(shù)########### #透過(guò)JavaScript 自身自動(dòng)轉(zhuǎn)換##################將數(shù)字轉(zhuǎn)換為字串############全域方法?String()?可以將數(shù)字轉(zhuǎn)換為字串。 ###

該方法可用於任何類(lèi)型的數(shù)字,字母,變量,表達(dá)式:

實(shí)例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p> String() 方法可以將數(shù)字轉(zhuǎn)換為字符串。</p>
<p id="demo"></p>
<script>
    var x = 123;
    document.getElementById("demo").innerHTML =
            String(x) + "<br>" +
            String(123) + "<br>" +
            String(100 + 23);
</script>
</body>
</html>

運(yùn)行程式嘗試


Number 方法?toString()?也是有相同的效果。

實(shí)例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>toString() 方法將數(shù)字轉(zhuǎn)換為字符串。</p>
<p id="demo"></p>
<script>
    var x = 123;
    document.getElementById("demo").innerHTML =
            x.toString() + "<br>" +
            (123).toString() + "<br>" +
            (100 + 23).toString();
</script>
</body>
</html>

執(zhí)行程式嘗試


數(shù)字轉(zhuǎn)換為字串的方法:

方法描述
#toExponential()把物件的值轉(zhuǎn)換為指數(shù)計(jì)數(shù)法。
toFixed()把數(shù)字轉(zhuǎn)換成字串,結(jié)果的小數(shù)點(diǎn)後面有指定位數(shù)的數(shù)字。
toPrecision()把數(shù)字格式化為指定的長(zhǎng)度。

將布林值轉(zhuǎn)換為字串

全域方法?String()?可以將布林值轉(zhuǎn)換為字串。

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

。

false.toString()?????// 返回"false"
true.toString()? ????// 返回"true"


將日期轉(zhuǎn)換為字串


全域方法?String()?可以將日期轉(zhuǎn)換為字串。

String(Date())??? ?+0200 (W. Europe Daylight Time)

##toString( )?也有相同的效果。

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

日期轉(zhuǎn)換為字串的函數(shù):

方法描述getDate()從Date 物件傳回一個(gè)月中的某一天(1 ~ 31)。 getDay()從 Date 物件傳回一週中的某一天 (0 ~ 6)。 getFullYear()從 Date 物件以四位數(shù)字傳回年份。 getHours()傳回 Date 物件的小時(shí) (0 ~ 23)。 getMilliseconds()傳回 Date 物件的毫秒(0 ~ 999)。 getMinutes()返回 Date 物件的分鐘 (0 ~ 59)。 getMonth()從 Date 物件傳回月份 (0 ~ 11)。 getSeconds()傳回 Date 物件的秒數(shù) (0 ~ 59)。 getTime()傳回 1970 年 1 月 1 日至今的毫秒數(shù)。

將字串轉(zhuǎn)換為數(shù)字

全域方法?Number()?可以將字串轉(zhuǎn)換為數(shù)字。

字串包含數(shù)字(如 "3.14") 轉(zhuǎn)換為數(shù)字 (如 3.14).

空字串轉(zhuǎn)換為 0。

其他的字串會(huì)轉(zhuǎn)換為 NaN (不是個(gè)數(shù)字)。

Number("3.14")????// 返回3.14
Number(" ")???????// 返回0?
Number(???88")???// 傳回NaN

字串轉(zhuǎn)為數(shù)字的方法:

方法parseFloat()parseInt()

一元運(yùn)算子+

Operator +?可用來(lái)將變數(shù)轉(zhuǎn)換為數(shù)字:

實(shí)例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p> typeof 操作符返回變量或表達(dá)式的類(lèi)型。</p>
<button onclick="myFunction()">點(diǎn)我</button>
<p id="demo"></p>
<script>
    function myFunction() {
        var y = "5";
        var x = + y;
        document.getElementById("demo").innerHTML =
                typeof y + "<br>" + typeof x;
    }
</script>
</body>
</html>

執(zhí)行程式嘗試一下


如果變數(shù)不能轉(zhuǎn)換,它仍然會(huì)是一個(gè)數(shù)字,但值為NaN (不是一個(gè)數(shù)字):

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP中文網(wǎng)(php.cn)</title>
</head>
<body>
<p> typeof 操作符返回變量或表達(dá)式的類(lèi)型。</p>
<button onclick="myFunction()">點(diǎn)我</button>
<p id="demo"></p>
<script>
function myFunction() {
    var y = "John";
    var x = + y;
    document.getElementById("demo").innerHTML =
typeof x + "<br>" + x;
}
</script>
</body>
</html>

執(zhí)行程式嘗試


將布林值轉(zhuǎn)換為數(shù)字

全域方法?Number()?可將布林值轉(zhuǎn)換為數(shù)字。

Number(false)?????// 返回0
Number(true)? ????// 返回1


將日期轉(zhuǎn)換為數(shù)字

#全域方法?Number()?可將日期轉(zhuǎn)換為數(shù)字。

d =?new?Date();
Number(d)??? ??????// 回傳 1404568027739

#日期方法

d =?new?Date();
d.getTime()??? ? ??// 返回1404568027739


自動(dòng)轉(zhuǎn)換類(lèi)型

#當(dāng)JavaScript 嘗試操作一個(gè)"錯(cuò)誤" 的資料型別時(shí),會(huì)自動(dòng)轉(zhuǎn)換為"正確" 的資料型態(tài)。

以下輸出結(jié)果不是你所期望的:

5?+?null????// 回傳5???????? null 轉(zhuǎn)換為0
"5"?+?null???? null 轉(zhuǎn)換為0
"5"?+?null???? null 轉(zhuǎn)換為0
"5"?+?null???? null 轉(zhuǎn)換為0

"5"?轉(zhuǎn)換為"null"
"5"?+?1?????// 返回"51"????? 1 轉(zhuǎn)換為"1"??

"5"?-?1???

##

自動(dòng)轉(zhuǎn)換成字串

#當(dāng)你嘗試輸出一個(gè)物件或變數(shù)時(shí)JavaScript 會(huì)自動(dòng)呼叫變數(shù)的toString() 方法:

document.getElementById("demo").innerHTML = myVar;

// if myVar = {name:"Fjohn"}? / / toString 轉(zhuǎn)換為"[object Object]"
// if myVar = [1,2,3,4]?????? // toString 轉(zhuǎn)換為"1,2,3,4"
// if myVar = new Date()????? // toString 轉(zhuǎn)換為"Fri Jul 18 2014 09:08:55 GMT+0200"

#數(shù)字和布林值也常互相轉(zhuǎn)換:



數(shù)字和布林值也?;ハ噢D(zhuǎn)換:


#// if myVar = 123????? ?????? // toString 轉(zhuǎn)換為"123"

// if myVar = true ???????????? // toString 轉(zhuǎn)換為"false" ################ 繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <p> typeof 操作符返回變量、對(duì)象、函數(shù)、表達(dá)式的類(lèi)型。</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = typeof "john" + "<br>" + typeof 3.14 + "<br>" + typeof NaN + "<br>" + typeof false + "<br>" + typeof [1,2,3,4] + "<br>" + typeof {name:'john', age:34} + "<br>" + typeof new Date() + "<br>" + typeof function () {} + "<br>" + typeof myCar + "<br>" + typeof null; </script> </body> </html>
提交重置程式碼
##說(shuō)明
解析一個(gè)字串,並傳回一個(gè)浮點(diǎn)數(shù)。
解析一個(gè)字串,並傳回一個(gè)整數(shù)。