Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请使用下面的代码用javascript向我解释原型_Javascript_Prototype - Fatal编程技术网

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

请使用下面的代码用javascript向我解释原型,javascript,prototype,Javascript,Prototype,我正在尝试使用编写函数的原型方法,这些函数可以通过字符串实现,以大写每个单词的第一个字母。我想把这个函数叫做 var str = "Example of a string"; str.toJadenCase(); var str = "Example of a string"; str.toJadenCase(); 这就是我试图编写的函数: String.prototype.toJadenCase = function () { //split the statement into

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

var str = "Example of a string";
str.toJadenCase();
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();
var str = "Example of a string";
str.toJadenCase();
不能,字符串是不可变的。你必须这样称呼它:

str = str.toJadenCase();
在函数中,未正确使用String.prototype。prototype是包含各种特定于字符串的方法的对象。它被指定为所有字符串的基础原型

当您使用String.prototype时,您应该使用它,而不是尝试将此=。。。无效,返回结果

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

将字符串拆分为一个单词数组,如下所示

循环遍历该数组,或者通过+=,用大写的单词构建一个新字符串,或者用大写的单词构建一个新数组,然后在末尾执行Arrayjoin,将其重新组合在一起

返回您构建的字符串

String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }
大概是这样的:

str = str.toJadenCase();
String.prototype.toJadenCase=函数{ var结果=此; //将语句拆分为每个单词 如果此值为0.length!==0{ 结果=this.split.mapfunctionword{ 返回word.substring0,1.toUpperCase+word.substring1; }.加入; } 返回结果; }; 这是一个test.toJadenCase; snippet.logoneword.toJadenCase; snippet.logblank:+.toJadenCase;
使用类似于RegExp和php的命名

str.ucwords


这是我如何做到的

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

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

这看起来像是一场代码战Katas-这是我的解决方案-

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

你的问题是什么?杰登?想要使用模因,但拒绝使用:pI尝试运行代码,但出现错误。你能找出任何一个吗?String.prototype=eachWord.join;是非常非常非常错误的。@AmitJoki…请随意使用it@fuyushimoya:啊!谢谢,没有仔细阅读,已修复。