Javascript String rev()
var a = "aa"; String.prototype.rev = function(){ console.log(this+"_rev_"+this.length); }; console.log("a"); a.rev();// w ww.j a v a 2 s. c o m
//Just like you can add methods to your own constructor, you can also add methods and properties to built in classes in JavaScript like Arrays and Objects. //Add a reverse method to the String 'class' so that every instance of String can call reverse and reverse itself. //code here/*from w w w . j a v a 2 s. c o m*/ String.prototype.rev = function() { return this.split("").reverse().join(""); }; var str = "class"; str.rev();
//Just like you can add methods to your own constructor, you can also add methods and properties to built in classes in JavaScript like Arrays and Objects. //Add a reverse method to the String 'class' so that every instance of String can call reverse and reverse itself. //code here// w ww. j a va 2 s .c o m String.prototype.rev = function() { var newStr = '' for (var i = this.length - 1; i >= 0; i--) { newStr += this[i]; } console.log(newStr); return newStr; } var testStr = 'just like you can add'; testStr.rev();