请使用下面的代码向我解释 javascript 中的原型
Please explain to me prototypes in javascript using the code below
我正在尝试使用原型方法编写可以由字符串实现的函数,将每个单词的每个首字母大写。我想这样调用这个函数,
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;
}
I would like to call this function like,
var str = "Example of a string";
str.toJadenCase();
你不能,字符串是不可变的。你必须这样称呼它:
str = str.toJadenCase();
在您的函数中,您使用的 String.prototype
不正确。 String.prototype
是包含各种 String
特定方法的对象。它被指定为所有字符串的基础原型。
在您使用 String.prototype
的地方,您应该使用 this
,而不是尝试分配给它(this = ...
无效),return 结果.
做你正在做的事情的简单方法是:
将字符串拆分为单词数组,就像您所做的那样
遍历该数组,或者通过 +=
构建一个包含大写单词的新字符串,或者使用大写单词构建一个新数组,然后在 Array#join
最后把它放回去。
Return你建的字符串
像这样:
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.ucwords()
String.prototype.ucwords = function() {
return this.replace(/\b\S/g,function(c){
return c.toUpperCase()
}
}
这是我的做法。
将字符串拆分成一个单词数组,就像你做的那样
循环遍历该数组,要么通过 += 使用大写单词构建一个新字符串,要么使用大写单词构建一个新数组,然后在最后执行 Array#join 将其放回原处。
Return 你构建的字符串
String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }
这看起来像是 Code Wars 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(' ');
};
我正在尝试使用原型方法编写可以由字符串实现的函数,将每个单词的每个首字母大写。我想这样调用这个函数,
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;
}
I would like to call this function like,
var str = "Example of a string"; str.toJadenCase();
你不能,字符串是不可变的。你必须这样称呼它:
str = str.toJadenCase();
在您的函数中,您使用的 String.prototype
不正确。 String.prototype
是包含各种 String
特定方法的对象。它被指定为所有字符串的基础原型。
在您使用 String.prototype
的地方,您应该使用 this
,而不是尝试分配给它(this = ...
无效),return 结果.
做你正在做的事情的简单方法是:
将字符串拆分为单词数组,就像您所做的那样
遍历该数组,或者通过
+=
构建一个包含大写单词的新字符串,或者使用大写单词构建一个新数组,然后在Array#join
最后把它放回去。Return你建的字符串
像这样:
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.ucwords()
String.prototype.ucwords = function() {
return this.replace(/\b\S/g,function(c){
return c.toUpperCase()
}
}
这是我的做法。
将字符串拆分成一个单词数组,就像你做的那样 循环遍历该数组,要么通过 += 使用大写单词构建一个新字符串,要么使用大写单词构建一个新数组,然后在最后执行 Array#join 将其放回原处。 Return 你构建的字符串
String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }
这看起来像是 Code Wars 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(' ');
};