How to add a method to String.prototype?

Trying to add a method to String.prototype. The new method should put a capital letter at the beginning of each word.

Did so, but it's really awkward, since you need to specify the argument in the function method. How to do that method was called without arguments?

String.prototype.toJadenCase = function (str) {

 str = str.toLowerCase().split(' ');

 for (let i = 0; i < str.length; i++) {
 str[i] = str[i].charAt(0).toUpperCase() + str[i].substring(1);
}

 return str = str.join(' ');
};


Checking method

var str = "How can mirrors be real if our eyes aren't real";
Test.assertEquals(str.toJadenCase(str), "How Can Mirrors Be Real If Our Eyes Aren't Real");
April 3rd 20 at 18:39
2 answers
April 3rd 20 at 18:41
Solution
Use this, not an argument.
I see that you are enlightened and know everything in this world. - haskell.Treutel commented on April 3rd 20 at 18:44
@haskell.Treutel, and I see that you tridecyl question on the sly. - Trevion_Friesen commented on April 3rd 20 at 18:47
@Trevion_Friesen, and I see that you tridecyl answer :) - sheldon_Dare77 commented on April 3rd 20 at 18:50
@sheldon_Dare77, a tooth for a tooth. - Trevion_Friesen commented on April 3rd 20 at 18:53
@Trevion_Friesen, thank you - haskell.Treutel commented on April 3rd 20 at 18:56
April 3rd 20 at 18:43
Solution
str = this.toLowerCase().split(' ');
Ah, thank you! - haskell.Treutel commented on April 3rd 20 at 18:46

Find more questions by tags JavaScript