Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > I can not get a strings replace() method to work

Reply
Thread Tools

I can not get a strings replace() method to work

 
 
Jake Barnes
Guest
Posts: n/a
 
      09-07-2006
I need to change imageUploadInfo2 to caption2 where the number at the
end is a variable that needs to be preserved (imageUploadInfo1 to
caption1, imageUploadInfo43 to caption43, etc)

Can anyone tell me why the following code doesn't work? In the alert at
the bottom I still get "imageUploadInfo2" (which starts off in
idOfInputThatNeedsToHaveItsImageInputUpdated).


idOfInputWhoseCaptionWeWillUpdate =
idOfInputThatNeedsToHaveItsImageInputUpdated;
var textToChange = "imageUploadInfo";
var regX = new RegExp(textToChange, "i");
idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");
alert("the input name is " + idOfInputWhoseCaptionWeWillUpdate);




I've also forgotten if Javascript passes values by value or by copy. I
assume by value, or my code is going to become a horrible mess.

 
Reply With Quote
 
 
 
 
Jeremy
Guest
Posts: n/a
 
      09-07-2006
Jake Barnes wrote:
> I need to change imageUploadInfo2 to caption2 where the number at the
> end is a variable that needs to be preserved (imageUploadInfo1 to
> caption1, imageUploadInfo43 to caption43, etc)
>
> Can anyone tell me why the following code doesn't work? In the alert at
> the bottom I still get "imageUploadInfo2" (which starts off in
> idOfInputThatNeedsToHaveItsImageInputUpdated).
>
>
> idOfInputWhoseCaptionWeWillUpdate =
> idOfInputThatNeedsToHaveItsImageInputUpdated;
> var textToChange = "imageUploadInfo";
> var regX = new RegExp(textToChange, "i");
> idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");
> alert("the input name is " + idOfInputWhoseCaptionWeWillUpdate);
>


You need to capture the return value of the replace function. It does
not replace the text inside the string on which it called; rather, it
returns a new string containing the old string with the specified
replacement performed.

This is probably what you want:

idOfInputWhoseCaptionWeWillUpdate =
idOfInputWhoseCaptionWeWillUpdate.replace(regX, "caption");

>
>
> I've also forgotten if Javascript passes values by value or by copy. I
> assume by value, or my code is going to become a horrible mess.
>


"By value" and "by copy" are essentially the same. Did you mean "By
value or by reference"?

Javascript usually passes by value. When you start working with DOM
nodes this gets a little fuzzy, but objects like strings, numbers, and
anonymous objects are passed by value if I'm not mistaken.

Jeremy
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      09-07-2006
Jeremy wrote:
[...]
> Javascript usually passes by value. When you start working with DOM
> nodes this gets a little fuzzy, but objects like strings, numbers, and
> anonymous objects are passed by value if I'm not mistaken.


Maybe confused

In an assignment expression, the value of the right hand side is
assigned to the left hand side. If the value is a primitive, then that
is assigned. If its value is a reference to an object, then a
reference is assigned.

e.g.

var x = 2; // x is assigned the value of 2
var y = x; // y is assigned the value of x, which is 2

var foo = {}; // foo is assigned a reference to an object
var bar = foo; // bar is assigned the value of foo,
// a reference to the same object
bar = y; // bar is now assigned the value of y, which is 2

HTH


--
Rob

 
Reply With Quote
 
scriptguru@gmail.com
Guest
Posts: n/a
 
      09-07-2006

RobG написав:
> Jeremy wrote:
> [...]
> > Javascript usually passes by value. When you start working with DOM
> > nodes this gets a little fuzzy, but objects like strings, numbers, and
> > anonymous objects are passed by value if I'm not mistaken.

>
> Maybe confused
>
> In an assignment expression, the value of the right hand side is
> assigned to the left hand side. If the value is a primitive, then that
> is assigned. If its value is a reference to an object, then a
> reference is assigned.
>
> e.g.
>
> var x = 2; // x is assigned the value of 2
> var y = x; // y is assigned the value of x, which is 2
>
> var foo = {}; // foo is assigned a reference to an object
> var bar = foo; // bar is assigned the value of foo,
> // a reference to the same object
> bar = y; // bar is now assigned the value of y, which is 2
>
> HTH
>

BTW it passes complex data type (objects/arrays) by reference and
primitive data types (string/number/boolean etc.) by value. Check the
JS specification.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-07-2006
scriptg...@gmail.com wrote:
> RobG написав:
> > Jeremy wrote:
> > [...]
> > > Javascript usually passes by value. When you start working with DOM
> > > nodes this gets a little fuzzy, but objects like strings, numbers, and
> > > anonymous objects are passed by value if I'm not mistaken.

> >
> > Maybe confused
> >
> > In an assignment expression, the value of the right hand side is
> > assigned to the left hand side. If the value is a primitive, then that
> > is assigned. If its value is a reference to an object, then a
> > reference is assigned.
> >
> > e.g.
> >
> > var x = 2; // x is assigned the value of 2
> > var y = x; // y is assigned the value of x, which is 2
> >
> > var foo = {}; // foo is assigned a reference to an object
> > var bar = foo; // bar is assigned the value of foo,
> > // a reference to the same object
> > bar = y; // bar is now assigned the value of y, which is 2
> >
> > HTH
> >

> BTW it passes complex data type (objects/arrays) by reference and
> primitive data types (string/number/boolean etc.) by value. Check the
> JS specification.


Is that intended to be a correction or a further explanation? The "="
operator is a simple assignment operator, not a "pass" opertator. It
assigns values, it doesn't "pass" them.

Check the ECMAScript Language Specification, Section 11.3.


--
Rob

 
Reply With Quote
 
scriptguru@gmail.com
Guest
Posts: n/a
 
      09-07-2006

RobG написав:
> scriptg...@gmail.com wrote:
> > RobG написав:
> > > Jeremy wrote:
> > > [...]
> > > > Javascript usually passes by value. When you start working with DOM
> > > > nodes this gets a little fuzzy, but objects like strings, numbers, and
> > > > anonymous objects are passed by value if I'm not mistaken.
> > >
> > > Maybe confused
> > >
> > > In an assignment expression, the value of the right hand side is
> > > assigned to the left hand side. If the value is a primitive, then that
> > > is assigned. If its value is a reference to an object, then a
> > > reference is assigned.
> > >
> > > e.g.
> > >
> > > var x = 2; // x is assigned the value of 2
> > > var y = x; // y is assigned the value of x, which is 2
> > >
> > > var foo = {}; // foo is assigned a reference to an object
> > > var bar = foo; // bar is assigned the value of foo,
> > > // a reference to the same object
> > > bar = y; // bar is now assigned the value of y, which is 2
> > >
> > > HTH
> > >

> > BTW it passes complex data type (objects/arrays) by reference and
> > primitive data types (string/number/boolean etc.) by value. Check the
> > JS specification.

>
> Is that intended to be a correction or a further explanation?

It was a shorter explanation.

var a=[1,2,3]
function x(arr){
arr.pop()
}
x(a)

Var passed by reference.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      09-08-2006
wrote:
> RobG напи ав:

<snip>
>> var foo = {}; // foo is assigned a reference to an object
>> var bar = foo; // bar is assigned the value of foo,
>> // a reference to the same object

<snip>
> BTW it passes complex data type (objects/arrays) by reference
> and primitive data types (string/number/boolean etc.) by value.
> Check the JS specification.


ECMA 262 states that both the right hand side of an assignment and the
Expressions in an arguments list are evaluated and the result passed as
an argument to the internal - GetValue - function, with the returned
value being the value assigned or the value used as the argument to a
function call. The specification also never states what the 'value' of
an object actually is. However, the practical upshot of this is that all
assignments are by value, and all function arguments are passed by
value, it is just that some of those values effectively 'refer' to
objects.

Richard.


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-08-2006
wrote:
> RobG написав:
> > scriptg...@gmail.com wrote:
> > > RobG написав:
> > > > Jeremy wrote:
> > > > [...]
> > > > > Javascript usually passes by value. When you start working with DOM
> > > > > nodes this gets a little fuzzy, but objects like strings, numbers, and
> > > > > anonymous objects are passed by value if I'm not mistaken.
> > > >
> > > > Maybe confused
> > > >
> > > > In an assignment expression, the value of the right hand side is
> > > > assigned to the left hand side. If the value is a primitive, then that
> > > > is assigned. If its value is a reference to an object, then a
> > > > reference is assigned.
> > > >
> > > > e.g.
> > > >
> > > > var x = 2; // x is assigned the value of 2
> > > > var y = x; // y is assigned the value of x, which is 2
> > > >
> > > > var foo = {}; // foo is assigned a reference to an object
> > > > var bar = foo; // bar is assigned the value of foo,
> > > > // a reference to the same object
> > > > bar = y; // bar is now assigned the value of y, which is 2
> > > >
> > > > HTH
> > > >
> > > BTW it passes complex data type (objects/arrays) by reference and
> > > primitive data types (string/number/boolean etc.) by value. Check the
> > > JS specification.

> >
> > Is that intended to be a correction or a further explanation?

> It was a shorter explanation.
>
> var a=[1,2,3]
> function x(arr){
> arr.pop()
> }
> x(a)
>
> Var passed by reference.


Sorry, but I find such explanations confusing and unhelpful - I'd
rather use the terms and expressions contained in the language
specification. If someone doesn't understand how an assignment
operator works, the above example won't help them at all.

In your use of:

var a = [1,2,3];

you should explain that "a" is assigned a reference to the Array object
created on the right. You might then have said that:

var b = a;

will result in b being assigned the same value as a, so that now b is a
reference to the same array. So the value of a variable is always
"passed by value". Saying that some are "passed by reference" and that
others are "passed by value" leads to a fundamental misunderstanding of
how assignment works and of what the value of a variable is.

I don't know where the term "passed" came from originally, but the use
of terminology from some other context assumes that the person
implicitly understands the same (unstated) context. They may well
spend more time trying to learn and apply the foreign terminology to an
inappropriate situation than in understanding the answer the original
question.

Recent discussions on JavaScript Objects and hash tables spring to
mind.


--
Rob

 
Reply With Quote
 
Jake Barnes
Guest
Posts: n/a
 
      09-08-2006

Jeremy wrote:
> Jake Barnes wrote:
> > I've also forgotten if Javascript passes values by value or by copy. I
> > assume by value, or my code is going to become a horrible mess.
> >

>
> "By value" and "by copy" are essentially the same. Did you mean "By
> value or by reference"?
>
> Javascript usually passes by value. When you start working with DOM
> nodes this gets a little fuzzy, but objects like strings, numbers, and
> anonymous objects are passed by value if I'm not mistaken.


Yes, thank you, that is what I meant. Been dabbling with Ruby lately,
forgetting which languages pass what.

 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      09-08-2006
RobG wrote:
> Jeremy wrote:
> [...]
>> Javascript usually passes by value. When you start working with DOM
>> nodes this gets a little fuzzy, but objects like strings, numbers, and
>> anonymous objects are passed by value if I'm not mistaken.

>
> Maybe confused
>
> In an assignment expression, the value of the right hand side is
> assigned to the left hand side. If the value is a primitive, then that
> is assigned. If its value is a reference to an object, then a
> reference is assigned.
>
> e.g.
>
> var x = 2; // x is assigned the value of 2
> var y = x; // y is assigned the value of x, which is 2
>
> var foo = {}; // foo is assigned a reference to an object
> var bar = foo; // bar is assigned the value of foo,
> // a reference to the same object
> bar = y; // bar is now assigned the value of y, which is 2
>
> HTH
>
>


Hmph! You're right! I just tested it out. I had always thought
anonymous objects were copied on assignment. I guess it's never been an
issue.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
i still can not get asp or asp.net to work on xp sp2 can some help with step by step craig dicker ASP .Net 1 07-10-2005 10:48 AM
Hi I am new to asp i can not get it to work on xp pro sp2 even though the localhost work but asp pages dont so can some one help craig dicker ASP .Net 9 07-07-2005 11:52 AM
Mechanize: Can't get it to work. Can I help make it work next week? Xeno Campanoli Ruby 1 07-01-2005 10:32 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments