How does js determine the data type of an object
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!