Number prototype Property - Javascript Number

Javascript examples for Number:prototype

Description

The prototype constructor can add new properties and methods to JavaScript numbers.

Number.prototype refers to the Number() object itself.

Syntax

The following code shows how to Create a new number method that returns a number's half value:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
Number.prototype.myMethod = function() {
    return this.valueOf() / 2;/*from w  ww  . j a  va2s .c  o  m*/
};

function myFunction() {
    var n = 55;
    document.getElementById("demo").innerHTML = n.myMethod();
}
</script>

</body>
</html>

Related Tutorials