Get the App
SLTechnology News&Howtos  ›  Development  › 

How does js determine the data type of an object

Shulou Source: shulou.com Published: 2022-06-01 03:49:12 11月27日 Update

This article mainly introduces js how to judge the data type of the object, the article is very detailed, has a certain reference value, interested friends must read it!

Determine the data type of the object

Use Object.prototype.toString with closures to determine the object data type:

Const isType = type = > target = > `[object ${type}]` = Object.prototype.toString.call (target) const isArray = isType ('Array') ([1,2,3]) console.log (isArray) > Result: true

The above code is equivalent to:

Function isType (type) {return function (target) {return `[object ${type}]` = Object.prototype.toString.call (target)}} isType ('Array') > Result: true

Or:

Const isType = type = > target = > `[object ${type}]` = Object.prototype.toString.call (target) const isString = isType ('String') const res = isString ((' 1')) console.log (res) > Result: true is all the contents of the article "how js determines the data type of an object". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Object data type content article code value interest guy partner more knowledge industry information information channel closure face channel reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Shulou Tech Info OPPO Reno MySQL