How to turn function into .prototype?

Viewed 53

Problem

I have created a function that breaks an array into sentences within an array.

But, at the moment, I can only do one array at a time. The objects name will be created created dynamically.

Is there anyway to convert the create_Sentences function into a .prototype.

This way, the create_Sentences function is dynamic.

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>

What I've tried

I've tried to execute something along these lines multiple times.

Yet it keeps failing.

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 Answers

sentences is an object, you need to add function in object prototype.

Example:

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>");

PS: comment if any questions.

If you want in String prototype, need to send sentences.all[0].

Example Snippet :

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>");

There's actually two problems here. The basic one is that

sentences.all

is an array, not a string, so you can't call your create_sentences() function against it. You could try

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

...or you could make create_sentences() into a function on the Array prototype.

The other issue is that the create_sentences() function is trying to alter a string object in place, which isn't actually possible because javascript strings are immutable. So when you do

this = modifiedText.split(c);

...it doesn't make any sense, because this can't be reassigned. What you're trying to do is make a change to a string object, but all you can do is create a modified copy. So I'd do

return modifiedText.split(c);

and call it with

var newSentence = oldSentence.create_sentences();

The reason is that you're modifying the String prototype after you've created the Strings you want to work on. Modifying a prototype only affects objects that are created after you do the modification (because object creation basically involves copying the prototype). So do your String.prototype.create_Sentences stuff first, and it should all work fine.

Related