JavaScript How to use custom created methods of a string prototype?

I created my own method which basically capitalizes the first alphabet of every word in a string.

However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where am I wrong?

String.prototype.toCapitalize = (str) => {
  let splits = str.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
h.toCapitalize();
Answer:1

How did you think that the first argument is the string? It is supposed to be this. Replacing str with this and works:

String.prototype.toCapitalize = function () {
  let splits = this.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
console.log(h.toCapitalize());
Answer:2

I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...

I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...

  1. multi action button apk
  2. multi action button
  3. multi action button ux
  4. multi action button pro apk
  5. multi action home button pro apk
  6. multi action home button apk
  7. multi action home button
  8. multi action home button pro
  9. multi action home button pro 2.2.8 apk
  10. multi floating action button android
  11. multi action home button pro apk download
  12. multi action home button pro version download
  13. multi action home button apk download
  14. multi action home button app
  15. multi floating action button flutter
  16. multi action home button mod apk
  17. bootstrap multi action button
  18. multi action home button pro mod apk
  19. multi action home button download
  20. multi action home button full version

How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...

How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...

I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...

I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...

I'm working on a project, and we use a lot of coding languages for different parts. I've tried to simplify the problem as much as I can, and came up with the following JSfiddle. The problem is, I ...

I'm working on a project, and we use a lot of coding languages for different parts. I've tried to simplify the problem as much as I can, and came up with the following JSfiddle. The problem is, I ...

  1. passing json string as query parameter
  2. passing json string to restful web service
  3. passing json string in url
  4. passing json string to mvc controller
  5. passing json string to spring mvc controller
  6. passing json string to javascript function
  7. passing json string as parameter to webmethod
  8. passing json string to render_template in flask
  9. passing json string to jsp
  10. passing json string
  11. passing json string in java
  12. send json string in post request
  13. send json string in post request c#
  14. send json string in get request
  15. send json string in postman
  16. send json string in post request java
  17. send json string in post request javascript
  18. send json string
  19. send json string ajax jquery
  20. send json string in post request postman