The prototype
constructor allows you to add new properties and methods to JavaScript numbers.
prototype |
Yes | Yes | Yes | Yes | Yes |
Number.prototype.name =value
The following code shows how to Create a new number method that returns a number's half value.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from w ww .ja va2s. c o m-->
<script>
Number.prototype.myMethod = function() {
return this.valueOf() / 2;
}
function myFunction() {
var n = 50;
document.getElementById("demo").innerHTML = n.myMethod();
}
</script>
</body>
</html>
The code above is rendered as follows: