Register  |  Login




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.

Question

Status: Closed Points: 50 Time: 13:31 - Sep 29, 2007  

jgivoni

How to set a String object's string value in JavaScript from within its methods?

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

Answer Discussion
Tutorials

 

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

jgivoni

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:


teddyjas: 50

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?

bryan

Date:: Feb 08, 2012

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

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?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

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! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es