Axel
Axel

Reputation: 5111

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();

Upvotes: 2

Views: 127

Answers (3)

CertainPerformance
CertainPerformance

Reputation: 370699

A couple of issues. The function takes a parameter of str, but you're not calling it with any parameter. The conventional way to reference the instantiated object you're calling a method on is to use this, but you have an arrow function - better to use a standard function so you can use this:

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());

But mutating the built-in objects is terrible practice - consider using a standalone function instead:

const toCapitalize = str => str
  .split(' ')
  .map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
  .join(' ');
let h = 'its a beautiful weather';
console.log(toCapitalize(h));

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

If you want to call toCapitalize as h.toCapitalize() then you need to use this.split(" "); as you are getting an error of str is not defined in console:

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());

Else, if you want to use the parameter str in the toCapitalize function then you need to call it as st.toCapitalize(h) where st can be any string type value.

String.prototype.toCapitalize = function(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 st ='';
let h = 'its a beautiful weather';
console.log(st.toCapitalize(h));

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

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());

Upvotes: 3

Related Questions