By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,569 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,569 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Sep 7 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Sep 7 '06 #2

P: n/a
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

Sep 7 '06 #3

P: n/a

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.

Sep 7 '06 #4

P: n/a
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

Sep 7 '06 #5

P: n/a

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.

Sep 7 '06 #6

P: n/a
sc********@gmail.com 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.
Sep 8 '06 #7

P: n/a
sc********@gmail.com 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

Sep 8 '06 #8

P: n/a

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.

Sep 8 '06 #9

P: n/a
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.
Sep 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.