1. 程式人生 > >js學習記錄

js學習記錄

string nbsp tostring on() function parse 構造函數 false obj

js 數據類型

typeof "John" // 返回 string
typeof 3.14 // 返回 number
typeof NaN // 返回 number isNaN(),判斷是否為非數值
typeof false // 返回 boolean
typeof [1,2,3,4] // 返回 object 數組比較特殊,返回object類型
typeof {name:‘John‘, age:34} // 返回 object
typeof new Date() // 返回 object
typeof function () {} // 返回 function
typeof myCar // 返回 undefined (如果 myCar 沒有聲明)
typeof null // 返回 object
undefined //undefined
constructor 屬性返回所有 JavaScript 變量的構造函數。你可以使用 constructor 屬性來查看對象是否為數組 (包含字符串 "Array"): "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] }
類型轉換, 數組轉換為數字。 Number("3.14") // 返回 3.14
Number(" ") // 返回 0
Number("") // 返回 0
Number("99 88") // 返回 NaN
轉換為字符串 String(), X.toString(). 字符串轉換為整數,parseInt("A") parseFloat()

js學習記錄