javascript typeof的用法與typeof運算符介紹

經常會在js裏用到數組,比如 多個名字相同的input, 若是動態生成的, 提交時就需要判斷其是否是數組. 
if(document.mylist.length != "undefined" ) {} 這個用法有誤. 
正確的是 if( typeof(document.mylist.length) != "undefined" ) {} 
或 if( !isNaN(document.mylist.length) ) {} 
typeof的運算數未定義,返回的就是 "undefined". 
運算數爲數字 typeof(x) = "number" 
字符串 typeof(x) = "string" 
布爾值 typeof(x) = "boolean" 
對象,數組和null typeof(x) = "object" 
函數 typeof(x) = "function" 
typeof 運算符返回一個用來表示表達式的數據類型的字符串。 
可能的字符串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。 
發佈了13 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章