js中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"。 
如: 
alert(typeof (123));//typeof(123)返回"number" 
alert(typeof ("123"));//typeof("123")返回"string"

 

二、
typeof 運算符

返回一個用來表示表達式的數據類型的字符串。


typeof[()expression[]] ;

expression 參數是需要查找類型信息的任意表達式


說明

typeof 運算符把類型信息當作字符串返回。typeof 返回值有六種可能: "number," "string," "boolean," "object," "function," 和 "undefined."

typeof 語法中的圓括號是可選項。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM