The prototype
constructor can add
new properties and methods to JavaScript booleans.
prototype |
Yes | Yes | Yes | Yes | Yes |
Boolean.prototype.name = value;
The following code shows how to add new method for JavaScript booleans type.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from w ww . j a va 2 s . c o m-->
<p id="demo"></p>
<script>
Boolean.prototype.toColor = function() {
if (this.valueOf() == true) {
return "black";
} else {
return "red";
}
}
function myFunction() {
var a = true;
document.getElementById("demo").innerHTML = a.toColor();
}
</script>
</body>
</html>
The code above is rendered as follows: