Joseph Erickson
Joseph Erickson

Reputation: 960

javascript and messing with the prototype of Strings

I'm trying to understand how to manipulate the prototype of the String class. I'm using the following code (which works):

String.prototype.reverseIt = function(someStr){
    someStr = someStr.split("");
  var newer = [];
  for(var i = 0; i<someStr.length; i++){
    newer.push(someStr[someStr.length - i - 1])
  }
  return newer;
}

var tester = "hello";

var x = "".reverseIt(tester);

console.log(x);

//Logs an array of "olleh"

What I'm trying to understand is, why do I have to have the "" before the invocation of .reverseIt, and how can I write this more effectively so that I can simply call reverseIt and pass it a string parameter without first stating "im a string" by using the "" before reverseIt is called?

Upvotes: 0

Views: 48

Answers (6)

guest271314
guest271314

Reputation: 1

how can I write this more effectively so that I can simply call reverseIt and pass it a string

function reverseIt(someStr){
    someStr = someStr.split("");
  var newer = [];
  for(var i = 0; i<someStr.length; i++){
    newer.push(someStr[someStr.length - i - 1])
  }
  return newer;
}

String.prototype.reverseIt = reverseIt;

reverseIt("hello")    

Alternatively, return String instead of returning Array

function reverseIt(someStr) {
  var i = someStr.length, res = "";
  while (--i > -1) { res += someStr[i]};
  // return `String` 
  return res
}

Upvotes: 1

user5230926
user5230926

Reputation:

Another option

    String.prototype.reverse = function(){
      var result = [];
      for(var i = this.length-1; i >= 0; i--){
        result.push(this[i])
      }
      return result.join('');
    }
    
    var tester = "hello";
    
    var x = tester.reverse();
    
    console.log(x);

Hope this help. However, it isn't a good practice to extend built-in JS classes.

For more on JS prototype, please read more here

Regards, K.

Upvotes: 0

StayCool
StayCool

Reputation: 552

Use the 'this' keyword to refer the the string itself.

String.prototype.reverseIt = function(){
  someStr = this.split("");
  var newer = [];
  for(var i = 0; i<someStr.length; i++){
    newer.push(someStr[someStr.length - i - 1])
  }
  return newer;
}

Invoke like so

var tester = "hello";

console.log(tester.reverseIt());

In your original function (not using 'this'), you could also call it like this:

String.prototype.reverseIt(tester);

Upvotes: 0

Travis Schettler
Travis Schettler

Reputation: 854

When you are extending the prototype, all strings will have that function by default, and this will be the string you call it on, so you can just do:

String.prototype.reverseIt = function(){
  return this.split('').reverse().join('');
}

var tester = "hello";

var x = tester.reverseIt();

console.log(x);

Upvotes: 0

Hatchet
Hatchet

Reputation: 5428

If you're adding a method to the prototype of an object, you can reference the this object inside of said method

String.prototype.reverseIt = function() {
    var someStr = this.split("");
    var newer = [];
    for (var i = 0; i < someStr.length; i++) {
        newer.push(someStr[someStr.length - i - 1])
    }
    return newer;
}

console.log("hello".reverseIt()); // Actually returns ["o", "l", "l", "e", "h"]

Since this returns an array, a .join("") call might be necessary at the end of your method.

return newer.join(""); // Convert the array to a string, separator character ""

Side note: If your only goal is to reverse the string, try:

"Hello".split("").reverse().join("");
  • split("") converts the string into an array of characters
  • reverse() reverses the array
  • join("") converts the array back into a string

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

Since you are working with prototype, you don't have to do it. Inside the method, this will refer to the string which called it, so you can call the method on the tester string itself instead of passing it as an argument

String.prototype.reverseIt = function() {
  var someStr = this.split("");
  var newer = [];
  for (var i = 0; i < someStr.length; i++) {
    newer.push(someStr[someStr.length - i - 1])
  }
  return newer;
}

var tester = "hello";

var x = tester.reverseIt();

document.body.innerHTML = x

//Logs an array of "olleh"

Upvotes: 0

Related Questions