Select Categories Below
Advertisement
Start Your Own Q&A Site
Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.
Status: Closed Points: 50 Time: 13:31 - Sep 29, 2007
jgivoni
A string is a string but a string is also a String Object in JavaScript. Therefore we can prototype it - I mean add methods to it using String.prototype. The question: How can I set the string's simple value from within one of it's methods? Imagine I want to add something to a string using a self-written method - something like this; var x = "Wayne's"; x.addSomething("World"); alert(x); // This should popup the message "Wayne's World"; I've tried with something like this method: String.prototype.addSomething = function(something) { this = this + " " + something; // This doesn't work. Can't assign a value to 'this' } How do I assign the new value to the string? Thanks, Jakob
Categories:
Add Categories
When adding more than one category, separate them with commas.
teddyjas
Date:: Apr 30, 2009
Time:: 02:08
why not using concat function? as its already in the string funtion var x = "Wayne's"; x.concat("World");
admin
Date:: May 22, 2009
Time:: 03:41
The question looks to be abandoned by the user who asked it. If no action is taken within 2 days, a Quomon Moderator will consider closing the question and distributing the points. The Quomon Team
Date:: Aug 30, 2009
Time:: 03:58
A short answer to my question seems to be that "In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings." (ref. http://stackoverflow.com/questions/51185...) Which probably explains why I cannot prototype it (i.e. add methods that act on and change the string). However, your suggestion, teddyjas, will indeed work in the specific example I gave. I do not at the moment understand how this relates to the immutability problem, but it might be another question. Since so much time has passed I'll award the points and close the question.
bryan
Date:: Feb 08, 2012
Time:: 17:24
Try defining a new object that contains the immutable string object. I like the name 'gstring' for general string (also a bit of a pun). You can then replace the internal reference at will. You could also use an array to represent each character of the string for maximal manipulation. All you then need is a to_string() method to make the final conversion.
Question Answered
This question has been closed, and points have been rewarded to the following experts:
You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.
Answer this Question
New User
Email:
Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address
Registered User
Username:
Password:
Forgot Your Password?
Enter your email address below and we will resend your login information to you.
Login Information Sent
Time:: 21:29
You can have your cake and eat it too! Simply encapsulate the string inside an object. Below is an example: function gstring(init) // general string object { // constructor var s=init; // accessor methods this.toString=function(){return s;}; this.to_s=function(){return s;}; this.to_n=function(){return parseFloat(s);}; this.size=function(){return s.length;}; this.clone=function(){return new gstring(s);}; // modifies self and returns this this.zap=function(){s=""; return this;}; this.concat=function(p){s+=p; return this;}; this.init=function(p){s=p; return this;}; // removes found section, returns left, and retains right this.parse=function(rgx) { var pos=s.search(rgx); if(pos < 0) // not found, return entire object { this.found=""; var rtn=gstring.new(s); s=""; return rtn; } var ary=s.match(rgx); var left=s.substr(0,pos); this.found=ary[0]; // preserve a copy s=s.substring(pos+this.found.length); return new gstring(left); }; } // optional constructor: gstring.new=function(init){return new gstring(init);} /////////////\\\\\\\\\\\\\\ now to create one you can do this: var myString = gstring.new("wow, now I have a string!"); my parse method looks for a token to split the string, keeps the right-hand side in the object, returns the left-side of the token, and allows you to access what token was found (in case you use an rxg). example: var left=myString.parse(','); // left == 'wow' alert('>>'+myString); // == '>> now I have a string!' Notice that gstring (pun intended) can be used in an expression as shown above. You can add more methods as needed, or copy the ones from javascript's string. As for the method above, concatenation is one of the methods.
Click here to see the Answer Discussion that preceded this tutorial.
Login to rate this tutorial: Good | Bad
Questions
How do I add methods to DOM element objects in javascript?
You have 100 characters to use
Rank
Expert
Points
1.
10354
2.
6493
3.
5596
4.
4848
5.
3487
6.
2840
7.
2770
8.
2303
9.
1820
10.
917
Register today to share your knowledge with the community and be recognized and rewarded for your contributions.
Register Here
"Psst, Quomon is a great site. Pass it on." Tell a Friend | Link To Us | Save to Delicious | Digg it
Language Options
English:
Español:
Sponsors
Questions and Answers Software