getTag returns string representation of Object.We may not expect the right value for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6 We expect DataView - [object DataView] Map - [object Map] Promise - [object Promise] Set - [object Set] WeakMap - [object WeakMap] In all the above cases where we don't get the value as expected, we derive at the object's type using its constructor. console.log(`${DataView}`) const o = new DataView(new ArrayBuffer(1)) console.log("Based on o.constructor") console.log(`${o.constructor}`) Output: function DataView() { [native code] } Based on o.constructor function DataView() { [native code] }
If the value is string, it can be converted to its numeric form using + . console.log(typeof "1") console.log(typeof +"1") If the value is symbol , NaN will be returned. Output: string number
Before we go for conversion, we can first try typeof value. If it returns string then we don't have to do any conversion. If the value is array then we need to convert each of its value to its string form. if (Array.isArray(value)) { return `${value.map(baseToString)}` } If it is a symbol then we need to convert it to symbol's toString. console.log(Symbol.prototype.toString.call(Symbol.toStringTag)) Output: Symbol(Symbol.toStringTag) Anything else we just need to parse. const value = true console.log(`${value}`) Output: true
Comments
Post a Comment