Add function to buildin type in JavaScript

Description

The following code shows how to add function to buildin type.

Example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function letterBoolean() {<!--from  www . jav  a  2s  .c om-->
if(this == true)
return("T");
else
return("F");
}
//Make the letterBoolean function available to all Boolean objects
Boolean.prototype.letter = letterBoolean;

var myBooleanObj = new Boolean(true);

document.write("myBooleanObj is set to ",myBooleanObj.letter());
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Add function to buildin type in JavaScript