473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

function arguments

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
Nov 21 '07 #1
18 1501
Ron Croonenberg said the following on 11/20/2007 11:15 PM:
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 ?
The first thing you are missing is the code that you think should have
changed strings in the first function. The second is an explanation of
what you think should be happening and what is happening.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 21 '07 #2
On Nov 20, 11:15 pm, Ron Croonenberg <r...@depauw.eduwrote:
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...
Nov 21 '07 #3
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.
>
The first thing you are missing is the code that you think should have
changed strings in the first function. The second is an explanation of
what you think should be happening and what is happening.
Randy Webb wrote:
Ron Croonenberg said the following on 11/20/2007 11:15 PM:
>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 ?

The first thing you are missing is the code that you think should have
changed strings in the first function. The second is an explanation of
what you think should be happening and what is happening.
Nov 21 '07 #4
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 ?
function first() {
var str1 = "";
var str2 = "";

if(something) {
str1 = "this"
} else if (something_else) {
str2 = "that"
}
}
Nov 21 '07 #5
On Nov 21, 5:41 pm, Ron Croonenberg <r...@depauw.eduwrote:
<snip>
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";
Nov 21 '07 #6
On Nov 20, 10:15 pm, Ron Croonenberg <r...@depauw.eduwrote:
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
Nov 21 '07 #7
Hi Matt,

thanks !

Ron

Matt Kruse wrote:
On Nov 20, 10:15 pm, Ron Croonenberg <r...@depauw.eduwrote:
>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
Nov 22 '07 #8
Matt Kruse wrote:
On Nov 20, 10:15 pm, Ron Croonenberg <r...@depauw.eduwrote:
>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.
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 23 '07 #9
On Nov 23, 11:44 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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

Nov 23 '07 #10
Matt Kruse wrote:
On Nov 23, 11:44 am, Thomas 'PointedEars' Lahn wrote:
>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.
<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.

Nov 24 '07 #11
Matt Kruse wrote:
On Nov 23, 11:44 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>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.
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 24 '07 #12
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".

Nov 24 '07 #13
VK wrote:
>"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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 24 '07 #14
VK
On Nov 24, 11:40 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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....470812c5e4d3b5

And now:
http://groups.google.com/group/comp....8271bfe4403d42
and
http://groups.google.com/group/micro...1e43b606f5cb1f
Nov 24 '07 #15
VK wrote:
On Nov 24, 11:40 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>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.
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 24 '07 #16
Richard Cornford wrote:
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
Nov 24 '07 #17
VK
On Nov 25, 12:30 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
What does "VK" stand for -- "Very Krude"?
That should be "Crude" then, is not it
Let's say "Verdankt Kultur(Mann)" <g>
Nov 24 '07 #18
VK wrote:
On Nov 25, 12:30 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>What does "VK" stand for -- "Very Krude"?

That should be "Crude" then, is not it
Not if the pun is understood.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 24 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
7
by: sfeher | last post by:
Hi All, Is there a way to preserve the arguments across functions? I have: <script> function myFirstFunction() { // arguments = 'param1'
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.