如何将函数转换为.prototype?

时间:2017-09-21 23:45:16

标签: javascript jquery

问题

我创建了function,将array分解为数组中的句子。

但是,目前,我一次只能做一个array。将动态创建objects名称。

无论如何都要将create_Sentences函数转换为.prototype

这样,create_Sentences函数是动态的。

sentences = {
  load: [],
  all: [
    "If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways."
  ]
}
graphic_designer = {
  logo_designer: [
    "For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block."
  ],
  grand_master: [
    "It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session."
  ],
  hello: [
    "Random sentences can also spur creativity in other types of projects being done. If you are trying to come up with a new concept, a new idea or a new product, a random sentence may help you find unique qualities you may not have considered. Trying to incorporate the sentence into your project can help you look at it in different and unexpected ways than you would normally on your own."
  ]
}




function create_Sentences() {
  a = /([.?!])\s+([a-zA-Z]|[$\-])/g
  b = "$1" + "newSentence newSentence" + "$2"
  c = "newSentence newSentence"
  modifiedText = sentences.all[0].replace(a, b)
  sentences.all = modifiedText.split(c)
}
create_Sentences()



document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
document.write("<pre>" + JSON.stringify(graphic_designer, null, 2) + "</pre>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我尝试了什么

我曾尝试多次沿着这些行执行某些操作。

然而它一直在失败。

sentences = {
  load: [],
  all: [
    "If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways."
  ]
}
graphic_designer = {
  logo_designer: [
    "For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block."
  ],
  grand_master: [
    "It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session."
  ],
  hello: [
    "Random sentences can also spur creativity in other types of projects being done. If you are trying to come up with a new concept, a new idea or a new product, a random sentence may help you find unique qualities you may not have considered. Trying to incorporate the sentence into your project can help you look at it in different and unexpected ways than you would normally on your own."
  ]
}



String.prototype.create_Sentences = function() {
  a = /([.?!])\s+([a-zA-Z]|[$\-])/g
  b = "$1" + "newSentence newSentence" + "$2"
  c = "newSentence newSentence"
  modifiedText = this[0].replace(a, b)
  this = modifiedText.split(c)
}


sentences.all.create_Sentences()
graphic_designer.logo_designer.create_Sentences()
graphic_designer.grand_master.create_Sentences()
graphic_designer.hello.create_Sentences()



document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
document.write("<pre>" + JSON.stringify(graphic_designer, null, 2) + "</pre>");

3 个答案:

答案 0 :(得分:3)

sentences是一个对象,你需要在对象原型中添加函数。

示例:

&#13;
&#13;
sentences = {
  load: [],
  all: [
    "If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways."
  ]
}
graphic_designer = {
  logo_designer: [
    "For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block."
  ],
  grand_master: [
    "It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session."
  ],
  hello: [
    "Random sentences can also spur creativity in other types of projects being done. If you are trying to come up with a new concept, a new idea or a new product, a random sentence may help you find unique qualities you may not have considered. Trying to incorporate the sentence into your project can help you look at it in different and unexpected ways than you would normally on your own."
  ]
}

Object.prototype.createSentences = function createSentences() {
  var a = /([.?!])\s+([a-zA-Z]|[$\-])/g;
  var b = "$1" + "newSentence newSentence" + "$2";
  var c = "newSentence newSentence";
  modifiedText = this.all[0].replace(a, b);
  return modifiedText.split(c)
}
document.write("<pre>" + JSON.stringify(sentences.createSentences(), null, 2) + "</pre>");
&#13;
&#13;
&#13;

PS:如果有任何问题请发表评论。

如果你想要String原型,需要发送sentences.all[0]

示例代码段:

&#13;
&#13;
sentences = {
  load: [],
  all: [
    "If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways."
  ]
}
graphic_designer = {
  logo_designer: [
    "For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block."
  ],
  grand_master: [
    "It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session."
  ],
  hello: [
    "Random sentences can also spur creativity in other types of projects being done. If you are trying to come up with a new concept, a new idea or a new product, a random sentence may help you find unique qualities you may not have considered. Trying to incorporate the sentence into your project can help you look at it in different and unexpected ways than you would normally on your own."
  ]
}

String.prototype.createSentences = function createSentences() {
  var a = /([.?!])\s+([a-zA-Z]|[$\-])/g;
  var b = "$1" + "newSentence newSentence" + "$2";
  var c = "newSentence newSentence";
  modifiedText = this.replace(a, b);
  return modifiedText.split(c)
}
document.write("<pre>" + JSON.stringify(sentences.all[0].createSentences(), null, 2) + "</pre>");
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这里实际上有两个问题。基本的是

sentences.all

是一个数组,而不是字符串,因此您无法针对它调用create_sentences()函数。你可以试试

sentences.all.forEach( s => s.create_sentences );

...或者您可以将create_sentences()放入Array原型的函数中。

另一个问题是create_sentences()函数试图改变一个字符串对象,这实际上是不可能的,因为javascript字符串是不可变的。所以当你这样做时

this = modifiedText.split(c);

......没有任何意义,因为this无法重新分配。您要做的是更改字符串对象,但您可以做的就是创建修改后的副本。所以我会做

return modifiedText.split(c);

并用

调用它
var newSentence = oldSentence.create_sentences();

答案 2 :(得分:0)

原因是您在创建了要处理的字符串之后修改了String原型。修改原型只会影响修改后创建的对象(因为对象创建基本上涉及复制原型)。那么首先你的String.prototype.create_Sentences东西,它应该都可以正常工作。