這篇文章主要介紹js怎樣判斷對象的數據類型,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
判斷對象的數據類型
使用Object.prototype.toString配合閉包來實現對象數據類型的判斷:
const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
const isArray = isType('Array')([1, 2, 3])
console.log(isArray)
> Result: true上面的代碼相當于:
function isType(type){
return function (target) {
return `[object ${type}]` === Object.prototype.toString.call(target)
}
}
isType('Array')([1,2,3])
> Result: true或者:
const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
const isString = isType('String')
const res = isString(('1'))
console.log(res)
> Result: true以上是“js怎樣判斷對象的數據類型”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。