Why extending Number
prototype works for 1.2
but not for 1
?
Number.prototype.to_s = function () {
return "" + this
}
console.log(1.2.to_s())
console.log(1.to_s())
Uncaught SyntaxError: Invalid or unexpected token
Why extending Number
prototype works for 1.2
but not for 1
?
Number.prototype.to_s = function () {
return "" + this
}
console.log(1.2.to_s())
console.log(1.to_s())
Uncaught SyntaxError: Invalid or unexpected token
When there is a .
in the end it is a part of the number.
You can use double dots to get past your issue for 1.
Number.prototype.to_s = function () {
return "" + this
}
let y = 1.2; // no issues;
let x = 1.; //no issues
console.log(x,y);
console.log(1..to_s());
console.log(1.2.to_s());
Also, there is a slight color difference in the two dots in the code snippet itself. So code editors also are pointing you towards the right syntax.