function arguments

R

Ron Croonenberg

Hello,

I am somewhat new to Javascript (except for the occasional short script
I never used it that that much...

Anyway, I am writing a script and have a function that need to change 2
strings.

basically in a function I have two strings (they are not global) str1
and str2:

function first () {
..
..
var str1 = "";
var str2 = "";
..
code..code...
..
second(str1, str1);
..
}


function wishfullthinking(str1, str2) {

if (something)
str1 = "this";
if (something else)
str2 = "that";
}

So I was hoping in first to have changed strings... what am I missing ?


thanks,

Ron
 
A

apatheticagnostic

Hello,

I am somewhat new to Javascript (except for the occasional short script
I never used it that that much...

Anyway, I am writing a script and have a function that need to change 2
strings.

basically in a function I have two strings (they are not global) str1
and str2:

function first () {
.
.
var str1 = "";
var str2 = "";
.
code..code...
.
second(str1, str1);
.

}

function wishfullthinking(str1, str2) {

if (something)
str1 = "this";
if (something else)
str2 = "that";

}

So I was hoping in first to have changed strings... what am I missing ?

thanks,

Ron

I assume that "second" is supposed to be a call to wishfullthinking?
If so, I think you should be able to rewrite your functions like so:

function first() {
var str1 = "";
var str2 = "";

function second() {
if(something) {
str1 = "this"
} else if (something_else) {
str2 = "that"
}
}

second();
}

You'll want to read about variable "scope" in javascript.

P.S. - my example may be off, I'm quite tired...
 
R

Ron Croonenberg

ok it was late, made a mistake explaining.

What I am trying to do is have a function take two strings as arguments
and change them.
 
R

Ron Croonenberg

Hi Apa....
I assume that "second" is supposed to be a call to wishfullthinking?
If so, I think you should be able to rewrite your functions like so:

Yes correct... (was late here too)

function first() {
var str1 = "";
var str2 = "";

function second() {
if(something) {
str1 = "this"
} else if (something_else) {
str2 = "that"
}
}

second();
}

You'll want to read about variable "scope" in javascript.

I did that.
var outside functions --> global
var inside functions --> local
no var inside functions --> global ??
P.S. - my example may be off, I'm quite tired...


Uhm I grew up coding in C. what is the point of declaring a function
inside another function ? I want that function second to be globally
accessible, and in this second can't be seen outside first ? ?
effectively your example is the same as below ? isn't it ?
 
H

Henry

What I am trying to do is have a function take two strings
as arguments and change them.
<snip>

Javascript string primitives are immutable (as are all of its
primitive values). Code such as:-

str1 = "this";

- replaces the pre-existing value of - str1 - with a new value, as
does code such as:-

str1 += "this";
 
M

Matt Kruse

Anyway, I am writing a script and have a function that need to change 2
strings.

The point being missed is that strings and numbers are always passed
by value, while objects are passed by reference.

This is kind of a ridiculous example, but you can take advantage of
the fact that global variables are just properties of the global
object (window):

var str1="abc";
var str2="xyz";
change('str1','str2');
alert(str1);
alert(str2);
function change(v1,v2) {
window[v1] = "ABC";
window[v2] = "XYZ";
}

Rather than passing in the strings themselves, this is passing in the
name of the variable, which is then used to de-reference the global
variable using the window object. Since that "points" to the same
global var, the original values are changed as you might expect.

Hope that helps,

Matt Kruse
 
R

Ron Croonenberg

Hi Matt,

thanks !

Ron

Matt said:
Anyway, I am writing a script and have a function that need to change 2
strings.

The point being missed is that strings and numbers are always passed
by value, while objects are passed by reference.

This is kind of a ridiculous example, but you can take advantage of
the fact that global variables are just properties of the global
object (window):

var str1="abc";
var str2="xyz";
change('str1','str2');
alert(str1);
alert(str2);
function change(v1,v2) {
window[v1] = "ABC";
window[v2] = "XYZ";
}

Rather than passing in the strings themselves, this is passing in the
name of the variable, which is then used to de-reference the global
variable using the window object. Since that "points" to the same
global var, the original values are changed as you might expect.

Hope that helps,

Matt Kruse
 
T

Thomas 'PointedEars' Lahn

Matt said:
The point being missed is that strings and numbers are always passed
by value, while objects are passed by reference.

Utter nonsense. As you have been explained before, (in ECMAScript
implementations) *all* values are passed by value. Object references
are *values*, but they are are only *references* *to* objects, not
the objects themselves.


PointedEars
 
M

Matt Kruse

Utter nonsense. As you have been explained before, (in ECMAScript
implementations) *all* values are passed by value. Object references
are *values*, but they are are only *references* *to* objects, not
the objects themselves.

Which is just another way to say that strings and numbers are passed
by value and objects are passed by reference. Thanks for backing me up
on that one.

If I didn't know you already, I would tell you to stop being so
obtuse.

Matt Kruse
 
R

Richard Cornford

Matt said:
Which is just another way to say that strings and numbers
are passed by value and objects are passed by reference.
Thanks for backing me up on that one.
<snip>

It has been pointed out before that it is entirely possible for
javascript implementations to pass all values by reference internally.
That is, it may be the case that all primitive values are internally
represented by objects and so all manifestations of primitive values
would actually be references to objects. Because javascript does not
have any operators that are capable of modifying a primitive value there
is no meaningful distinction between passing them by value and passing
them by reference (except that copying a reference to an object that
represents a primitive values and passing that is going to be more
efficient that creating a copy of the object that represents the
primitive value). And because the value of an object is a reference to
that object there is also no meaningful distinction between their being
passed by reference or passed by value (a copy of a reference to an
object is still a reference to that object).

The only significant details are that when an object is passed the thing
that is received is never a copy of that object but is instead 'the
object itself', and so operations preformed on that object will affect
that object, and that no operations can modify a primitive value in any
way (only replace it with another value).

Richard.
 
T

Thomas 'PointedEars' Lahn

Matt said:
Which is just another way to say that strings and numbers are passed
by value and objects are passed by reference.

Not at all.
Thanks for backing me up on that one.

I did not back you up at all, quite the opposite. You have just not
understood that "pass by value" and "pass by reference" are terms in
programming that have a fixed meaning which simply do not apply to
ECMAScript implementations. References to ECMAScript objects are
values, and they are passed by value.
If I didn't know you already, I would tell you to stop being so
obtuse.

If I did not know already you would be an ignorant, I would tell you so now.


PointedEars
 
V

VK

"pass by value" and "pass by reference" are terms in
programming that have a fixed meaning which simply do not apply to
ECMAScript implementations. References to ECMAScript objects are
values, and they are passed by value.

I guess you have a highly exaggerated idea of what "pass by value" and
"pass by reference" are: something like "by value is by value" but "by
reference" being something completely special, half-mysterious close-
to-godness creature of "big languages" :)

In either case the "value" (what the program internally gets) is
something like ARRAY0xFFFFF00FF or STRING0xFFFFF00FF
What exactly, it be depends on the language and the particular engine:
Perl, Gecko JavaScript, JScript etc. If you are really curious I may
tell you the exact format of the reference for the engine of your
choice. In either case it is a pointer to the stored value, so
internally any language is working "by reference". The difference
arises on the higher level only because some references, no mater how
many times passed, will point to the same value, so it is going to be
"by reference": change one - change everything. For other calls a new
copy of the item will be created, allocated and its reference (not the
original one) sent to consumer. That will be "by value".
 
T

Thomas 'PointedEars' Lahn

VK said:
"pass by value" and "pass by reference" are terms in
programming that have a fixed meaning which simply do not apply to
ECMAScript implementations. References to ECMAScript objects are
values, and they are passed by value.

I guess you have a highly exaggerated idea of what "pass by value" and
"pass by reference" are: something like "by value is by value" but "by
reference" being something completely special, half-mysterious close-
to-godness creature of "big languages" :) [...]

I am pretty sure that you are in no position at all to lecture anyone about
programming languages and the correct use of terms in that field, given your
ongoing misconceptions and fairytale stories regarding that very field.


PointedEars
 
V

VK

I am pretty sure that you are in no position at all to lecture anyone about
programming languages and the correct use of terms in that field, given your
ongoing misconceptions and fairytale stories regarding that very field.

The "story teller" in this thread is you so far, not me. So do you
want to see how does it really work in a script engine or is it enough
for you to know how, it must work by your own opinion? ;-)

P.S. An old story now, keep moving, but it just dropped some salt on
an old wound by recent posts: how had many "misconceptions and
fairytale stories by VK" been made:

First like that (for future quote linking):
http://groups.google.com/group/comp..._frm/thread/5e16300292d26e2e/b1470812c5e4d3b5

And now:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/568271bfe4403d42
and
http://groups.google.com/group/microsoft.public.scripting.jscript/browse_frm/thread/d81e43b606f5cb1f
 
T

Thomas 'PointedEars' Lahn

VK said:
The "story teller" in this thread is you so far, not me.

Even when I thought you could not be more ridiculous, you prove me wrong.
So do you want to see how does it really work in a script engine or is it enough
for you to know how, it must work by your own opinion? ;-)

I don't care about the internals of any script engine because that is not
the point here. Object references are considered values in ECMAScript
implementations. The specification makes that very clear; there is even a
Reference type.

What is passed here is not an object by reference but an object reference
by value. These are completely different concepts. Meaning that within the
local context the argument is a copy of that Reference value, which is why
applying `delete' on that argument or assigning `null' to it does not remove
the object. However, if the object was passed "by reference" as that term
is generally understood in programming, that object would cease to exist.
[Links to VK threads]

And I could not care less about your demonstrating your misconceptions and
being proven wrong, if there were not equally uninitiated people who could
be deceived by them.

What does "VK" stand for -- "Very Krude"?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Richard said:
The only significant details are that when an object is passed the thing
that is received is never a copy of that object but is instead 'the
object itself',

With proper regard to the quotes. The "thing that is received" is instead
a copy of the Reference value. Say, `r1' is a reference to an object `O':

r1 ---> O

If `r1' is passed like this:

function foo(r2)
{
// ...
}

foo(r1);

Then r2 will be a reference to O as well within the local execution context
of `foo':

r1 ---> O <--- r2
and so operations preformed on that object will affect that object,

That happens because both values refer to the same object.
and that no operations can modify a primitive value in any way (only
replace it with another value).

That happens because a primitive value is not a Reference value and vice-versa.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top