javascript使用toString方法判斷對象類型

function getType(o) {
    var _t;
    return ((_t = typeof (o)) == "object" ? o == null && "null"
            || Object.prototype.toString.call(o).slice(8, -1) : _t)
            .toLowerCase();
}

執行結果:


getType("abc"); //string
getType(true); //boolean
getType(123); //number
getType([]); //array
getType({}); //object
getType(function(){}); //function
getType(new Date); //date
getType(new RegExp); //regexp
getType(Math); //math
getType(null); //null
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章