API · JavaScript
JavaScript boolean.prototype Property
The boolean.prototype is used to add new properties and methods to Boolean.
In JavaScript, the .prototype property is available with all objects. This method allows us to add new properties to objects.
Syntax of JavaScript boolean.prototype Property
boolean.prototype.func = function(){
if(this.valueOf()){
/* perform some operation */
}
}
let val = false;
val.func();
Parameters
val |
It is a Boolean value on which we need to call any function defined in boolean.prototype. |
this |
Users can access the value of the val variable using the this keyword. |
Return
This method returns the value or function used to create the prototype.
Example 1: Use boolean.prototype to Get the Value Added With Different Object
In JavaScript, the boolean.prototype property allows us to add custom value or function as a prototype for different objects.
In this example, we have created an empty object and added a custom value that will be added to the output by using the boolean.prototype property.
function Obj(){}
const object = new Obj();
Obj.prototype.value = "Add word";
console.log(object.value);
Output:
Add word
Example 2: Use boolean.prototype With the Function to Create a Prototype for the Object
In JavaScript, the boolean.prototype property uses a function to create the prototype for an object. In this example, we have created an object and a function.
Then, we used the boolean.prototype property to set a prototype for the object.
Boolean.prototype.setvalue = function() {
if (this.valueOf() == false){
return "add value";
} else {
return " function";
}
};
let object = false;
console.log(object.setvalue());
Output:
add value
The boolean.prototype method is supported in all browsers.