Allen Marshall Allen Marshall - 1 month ago 11
Javascript Question

How to turn function into .prototype?

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




A.J A.J
Answer Source

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