请使用下面的代码向我解释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;
}
我想把这个函数称为
var str = "Example of a string"; str.toJadenCase();
不能,字符串是不可变的。你必须这样称呼它:
str = str.toJadenCase();
在您的函数中,您错误地使用了String.prototype
。String.prototype
是包含各种特定于String
的方法的对象。它被指定为所有字符串的底层原型。
在使用String.prototype
的情况下,应该使用this
,而不是尝试分配给它(this = ...
无效(,返回结果。
做你正在做的事情的简单方法是:
将字符串拆分为单词数组,就像一样
循环遍历该数组,或者通过
+=
用大写单词构建一个新字符串,或者用大写单词创建一个新数组,然后在最后执行Array#join
将其重新组合在一起。返回您构建的字符串
类似这样的东西:
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(" ");
};
相关文章:
- 你能解释一下为什么这个javascript不正确吗
- Javascript 如何强制 string.replace 不将我的字符串解释为正则表达式
- Appcelerator Titanium:在运行时下载并解释JavaScript代码
- 无法解释的JavaScript行为-错误
- 请为javascript解释MVC中的VC
- 解释javascript中的+var和-var一元运算符
- 需要解释Javascript regex请求参数解析
- 解释 JavaScript 中数字的行为
- 你能用javascript解释对象和数组运算吗
- 有人可以用javascript解释以下代码吗?它用于读取文本文件,但我不知道XML和AJAX的东西
- Javascript解释 - 语法/格式/风格对我来说是新的
- 有人能用Javascript解释这种奇怪的行为吗
- 有人能用Javascript解释一下这个语法吗;type=type||'fx'&”;
- 最大数字(javascript)解释
- 用JavaScript解释用户输入的日期
- 需要涉及函数的快速JavaScript解释
- 当年份为yy格式时,我如何用Javascript解释正确的日期
- 是JavaScript解释还是JIT编译
- 有人能用JavaScript解释一下这个函数吗?
- 是否有一种方法可以使用命令行工具来查看网页的JavaScript解释源