Hi,
By using prototypes in Javascript we can build our own methods with our own logic.Here in the following code i made two methods for comparing the string with starwith and endwith.
<script>
if ( typeof String.prototype.startsWith != 'function' ) {
String.prototype.startsWith = function( str ) {
return this.substring( 0, str.length ) === str;
}
};
alert( "hello world".startsWith( "hello" ) );
if ( typeof String.prototype.endsWith != 'function' ) {
String.prototype.endsWith = function( str ) {
return this.substring( this.length - str.length, this.length ) === str;
}
};
alert( "hello world".endsWith( "world" ) );
</script>
By using prototypes in Javascript we can build our own methods with our own logic.Here in the following code i made two methods for comparing the string with starwith and endwith.
<script>
if ( typeof String.prototype.startsWith != 'function' ) {
String.prototype.startsWith = function( str ) {
return this.substring( 0, str.length ) === str;
}
};
alert( "hello world".startsWith( "hello" ) );
if ( typeof String.prototype.endsWith != 'function' ) {
String.prototype.endsWith = function( str ) {
return this.substring( this.length - str.length, this.length ) === str;
}
};
alert( "hello world".endsWith( "world" ) );
</script>
No comments:
Post a Comment