Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops.

Extending JavaScript objects with custom functions

1 min read

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>

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus