I thought at first you wanted to do this for all numbers, which is answered here.
But if you only want to do this for one specific number, you can, but you probably won't want to. 99.9999999999998% of all numbers you work with in JavaScript are primitives, not objects, and so they don't have any properties (the ones they seem to have only come from Number.prototype
[or, in turn, from Object.primitive
], which the JavaScript engine will use if you try to access a property on a number primitive).
You could make a number object instead, and put the property on that:
const c = new Number(Math.PI);
c.isMultipleOfPI = true;
console.log(c);
console.log(String(c));
console.log(c.isMultipleOfPI);
The problem with that, though, is demonstrated by the console.log(c);
line above. With Chrome's devtools (and perhaps others), you see this:
{
isMultipleOfPI: true,
}
rather than 3.141592653589793
as you might be expecting.
The reason is that c
is an object, not a primitive, and some things that use it (such as console.log
) may treat it primarily as an object rather than primarily as a number.
Math and string concatenation work, though:
const c = new Number(Math.PI);
c.isMultipleOfPI = true;
console.log(c * 2);
console.log(c - 2);
console.log(c + 2);
console.log(c / 2);
console.log("The number is " + c);
Just beware that Number
instances (objects) are objects, and that very, very little code is written explicitly to work with them properly. Most code using numbers expects number primitives.
Alternatively, you could make an accessor property on Number.prototype
(Pointy shows you how) that determines if the number it's called on is a multiple of PI and returns the right flag. That would give you the syntax you showed, but on all numbers. But it is a function call, so accessing the property works it out each time. Just having a isMultipleOfPI(num)
function might be clearer.
In a comment you've written:
Because Number.prototype = function(){ return self % Math.PI ~ 0 }
is not what I'm looking for. I would wish to distinguish between 3.14
vs 3.14
with isMultipleOfPi property.
The only way to do that is to use Number
objects rather than number primitives, as described above.