Define a primitive data type: Difference between revisions
Content added Content deleted
(added vb; moved "omit from" reason from comment to tag arg) |
(→{{header|JavaScript}}: Handling for non-integer values, added method toString()) |
||
Line 609: | Line 609: | ||
<lang JavaScript>function Num(n){ |
<lang JavaScript>function Num(n){ |
||
n = Math.floor(n); |
|||
if(isNaN(n)) |
|||
throw new TypeError("Not a Number"); |
|||
if(n < 1 || n > 10) |
if(n < 1 || n > 10) |
||
throw new TypeError("Out of range"); |
throw new TypeError("Out of range"); |
||
Line 614: | Line 617: | ||
} |
} |
||
Num.prototype.valueOf = function() { return this._value; } |
Num.prototype.valueOf = function() { return this._value; } |
||
Num.prototype.toString = function () { return this._value.toString();} |
|||
var w = new Num(3), x = new Num(4); |
var w = new Num(3), x = new Num(4); |