Skip to content
Advertisement

Please explain to me prototypes in javascript using the code below

I am trying to use the prototype method of writing functions that can be implemented by strings to capitalise every first letter of every word. I would like to call this function like,

var str = "Example of a string";
str.toJadenCase();

This is the function I am trying to write:

String.prototype.toJadenCase = function () {
    //split the statement into each word
    if (String.prototype.length !== 0)
    {
        var eachWord = String.prototype.split(" ");
        var n = eachWord.length;

        if(n !== 0)
        {
            //loop through the array of words
            for(var i = 0; i < eachWord.length; i++){
                //for each loop, split the word into individual characters
                var charArray = eachWord[i].split("");

                //capitalise the first element of character array
                charArray[0] = charArray[0].toUpperCase();

                //join all elements in character array to string
                eachWord[i] = charArray.join("");
            }

            //join all the words to form the statement
            String.prototype = eachWord.join(" ");
            return String.prototype;
        }
    }
};

I had written it this way before:

var capitaliseInitial = function(sampleText){
    var textString = sampleText;
    //split the statement into each word
    var eachWord = textString.split(" ");

    //loop through the array of words
    for(var i = 0; i < eachWord.length; i++){
        //for each loop, split the word into individual characters
        var charArray = eachWord[i].split("");

        //capitalise the first element of character array
        charArray[0] = charArray[0].toUpperCase();

        //join all elements in character array to string
        eachWord[i] = charArray.join("");
    }
    //join all the words to form the statement
    textString = eachWord.join(" ");
    return textString;
}

Advertisement

Answer

I would like to call this function like,

var str = "Example of a string";
str.toJadenCase();

You can’t, strings are immutable. You would have to call it like this:

str = str.toJadenCase();

In your function, you’re using String.prototype incorrectly. String.prototype is the object containing the various String-specific methods. It’s assigned as the underlying prototype of all strings.

Where you’re using String.prototype, you should be using this, and instead of trying to assign to it (this = ... is invalid), return the result.

The simple way to do what you’re doing is to:

  1. Split the string into an array of words, as you have

  2. Loop through that array either building up a new string with the capitalized words via +=, or building a new array with the capitalized words and then doing Array#join at the end to put it back together.

  3. Return the string you built

Something like this:

String.prototype.toJadenCase = function() {
  var result = this;

  //split the statement into each word
  if (this.length !== 0) {
    result = this.split(" ").map(function(word) {
      return word.substring(0, 1).toUpperCase() + word.substring(1);
    }).join(" ");
  }

  return result;
};
snippet.log("this is a test".toJadenCase());
snippet.log("oneword".toJadenCase());
snippet.log("blank: " + ("".toJadenCase()));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Note I’ve done away with the check if the array of words’ length isn’t 0: It can’t be 0 if you’ve pre-checked the length as you have.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement