Boolean prototype Constructor - Javascript Boolean

Javascript examples for Boolean:prototype

Description

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

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

The following code shows how to Make a new method for JavaScript booleans:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
Boolean.prototype.myValue = function() {
    if (this.valueOf() == true) {/*from   ww w .j a  v  a2  s  . com*/
        return "yes";
    } else {
        return "no";
    }
};

function myFunction() {
    var a = true;
    document.getElementById("demo").innerHTML = a.myValue();
}
</script>

</body>
</html>

Related Tutorials