请使用下面的代码向我解释javascript中的原型

Please explain to me prototypes in javascript using the code below

本文关键字:javascript 解释 原型 代码 请使用      更新时间:2023-09-26

我正在尝试使用原型方法编写可以由字符串实现的函数,以大写每个单词的每个第一个字母。我想把这个函数称为

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

这是我试图写的函数:

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;
        }
    }
};

我以前是这样写的:

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;
}

我想把这个函数称为

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

不能,字符串是不可变的。你必须这样称呼它:

str = str.toJadenCase();

在您的函数中,您错误地使用了String.prototypeString.prototype是包含各种特定于String的方法的对象。它被指定为所有字符串的底层原型。

在使用String.prototype的情况下,应该使用this,而不是尝试分配给它(this = ...无效(,返回结果

做你正在做的事情的简单方法是:

  1. 将字符串拆分为单词数组,就像一样

  2. 循环遍历该数组,或者通过+=用大写单词构建一个新字符串,或者用大写单词创建一个新数组,然后在最后执行Array#join将其重新组合在一起。

  3. 返回您构建的字符串

类似这样的东西:

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>

注意,如果单词数组"length"不是0,我已经取消了检查:如果您已经预先检查了长度,则不可能是0

使用RegExp和类似php的命名

str.ucswords((

String.prototype.ucwords = function() {
    return this.replace(/'b'S/g,function(c){
        return c.toUpperCase()
    }
}

以下是我的操作方法。

将字符串拆分为单词数组,如下所示循环遍历该数组,或者通过+=用大写单词构建一个新字符串,或者用大写单词创建一个新数组,然后在末尾执行array#join将其重新组合在一起。返回您构建的字符串

String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }

这看起来像一个代码战争卡塔-这是我的解决方案-

String.prototype.toJadenCase = function () {
    // assign 'this' keyword to a variable and split String into an array
    var result = this.split(" ");
    /* loop through the array changing first character of each item to 
    uppercase & adding it to the remaining letters in each item */
    for(i = 0; i < result.length; i++) {
    result[i] = result[i].charAt(0).toUpperCase() + result[i].substring(1);
    }
    //finally return items joined back together in a string
    return result.join(' ');
};

另一种方法是:

String.prototype.toJadenCase = function() {
  return this
    .split(" ")
    .map(i => i.replace(i[0], i[0].toUpperCase()))
    .join(" ");
};