Extending JavaScript objects with custom functions

It is nice how JavaScript objects can be extended with additional custom functions. Today for instance I needed a "contains" function (as it exists already on Java String objects). This can be achieved very nicely as the example below demonstrates:
<html>
<head>
<script type="text/javascript">
String.prototype.contains = function(someString){
return (this.indexOf(someString) >= 0) ? true : false;
}

function test(){
var stringA = new String('Hallo');
var stringB = 'llo';
alert(stringA.contains(stringB));
}
</script>
</head>
<body>
<a href="#" onClick="javascript:test();">Click me!</a>
</body>
</html>
Kindle

Comments

0

Your ad here?