You can use it as the property name of an object.
function toString(value) {
var obj = {};
obj[value] = true;
return Object.getOwnPropertyNames(obj)[0];
}
console.log(toString(123));
console.log(toString(1.23));
console.log(toString(NaN));
console.log(Infinity);
console.log(toString(-0));
console.log(toString(1e99));
Run codeHide resultYou can also use the DOM attributes:
var obj = document.createElement('div');
obj.setAttribute('data-toString', value);
return obj.getAttribute('data-toString');
Or join the array
return [value].join();
And big and so on. There are many things that internally use the abstract ToString operation .
source
share