How to use prototype in javascript thatinherit from string Object and implements custom function -
i have funtionas follow,
function revstr(s) { ns = ''; (i = s.length - 1; >= 0; i--) ns += s[i]; alert(ns); }
i have been asked implement using prototype totally unaware of in javascript(i phpdeveloper:( )
i have create object inherits string object . think can
var mystring= new string();
and implement function revstr
using somethingcalled prototype
in javascript
can me out in .
thanks
that means want add revstr()
string.prototype
, strings can revstr()
.
you can implement function string prototype by:
string.prototype.revstr = function(){ ns = ''; (i = this.length - 1; >= 0; i--) ns += this[i]; alert(ns); }
so can use function on string afterwards this:
'hello world'.revstr(); 'fooobarrrr'.revstr();
Comments
Post a Comment