function: arguments by reference

T

Tom de Neef

The tutorials are very superficial when it comes to passing variables to
functions and I can not find an answer to this (engineered) example:

function previousElement(element, list, previous)
// should return true if element found in list
// in that case previous should get value of element at lower place in list
{ var k
for (k=1; k<list.length; k++)
{ if list[k] == element { previous = list[k-1]; return true } }
return false
}

In other words: how do you specify that previous should be called by
reference, so that a change within the function is reflected in the value of
previous outside the function. This would be the case when previous is an
object. But in this case it is a string or an integer.

Thank you
Tom
 
R

Richard Cornford

Tom said:
The tutorials are very superficial when it comes to passing
variables to functions and I can not find an answer to this
(engineered) example:

function previousElement(element, list, previous)
// should return true if element found in list
// in that case previous should get value of element at lower place
in list { var k
for (k=1; k<list.length; k++)
{ if list[k] == element { previous = list[k-1]; return true } }
^ ^

It really is a good idea to only post code here that would (at minimum)
execute if tried out. Otherwise you risk wasting a lot of other people's
time.
return false
}

In other words: how do you specify that previous should be called
by reference,

The only things in javascript that get called are functions.
so that a change within the function is reflected in the
value of previous outside the function. This would be the
case when previous is an object.

No it would not. An assignment to the - previous - identifier inside the
function would only assign a new value to the property of the function
call's Variable object with the name previous. You mean that assigning to a
property of an object referred to by the previous property of the Variable
object would result in its value being changed.
But in this case it is a string or an integer.

Javascript has no operators that are capable of modifying its primitive
values (so including strings and numbers). That renders the question moot.

In the case of your example a more practical strategy would be to take
advantage of the function's return value and have it return the item form
the list, if found, or null if not found. You can both test the return value
for trueness (elements, being objects, type-convert to true while null
type-converts to false) and end up with the value you are looking for, to do
with as you will.

Richard.
 
L

Lasse Reichstein Nielsen

Tom de Neef said:
In other words: how do you specify that previous should be called by
reference, so that a change within the function is reflected in the value of
previous outside the function.

Parameters in Javascript is exclusively passed by value (i.e.,
function calls are call-by-value). You cannot pass a variable,
only its value, so there is no way a method can change a variable
that it doesn't have in its own scope. The variable "previous" at
the call point and the parameter "previous" of the function are
completely unrelated, whatever their names are.

There are ways around this, but none of them pretty.

The typical way to implement a function like your example is to have
it return the previous element if it exists, and return undefined
if it doesn't (or even throw an error, if not-existing shouldn't
happen). Returning values by assigning to reference-passed variables
is always dangerous - mostly because it makes code harder to read if
you can't easily see where a local variable is changed.
This would be the case when previous is an object. But in this case
it is a string or an integer.

No, all parameters are passed by value, both object references and
primitive types. You can change the content of an object that you
are passed a reference to, but that's a different thing completely.

/L
 
D

dhtml

Tom said:
The tutorials are very superficial when it comes to passing
variables to functions and I can not find an answer to this
(engineered) example:
function previousElement(element, list, previous)
// should return true if element found in list
// in that case previous should get value of element at lower place
in list { var k
for (k=1; k<list.length; k++)
{ if list[k] == element { previous = list[k-1]; return true } }

^ ^

It really is a good idea to only post code here that would (at minimum)
execute if tried out. Otherwise you risk wasting a lot of other people's
time.
return false
}
In other words: how do you specify that previous should be called
by reference,

The only things in javascript that get called are functions.
so that a change within the function is reflected in the
value of previous outside the function. This would be the
case when previous is an object.

No it would not. An assignment to the - previous - identifier inside the
function would only assign a new value to the property of the function
call's Variable object with the name previous.



The Variable object's |previous| property gets a new value.

The Variable object doesn't get a new property.

|previous| is a Reference, but a only of the Variable object. This is
a little more than the OP is asking for thouhg.

You mean that assigning to a
property of an object referred to by the previous property of the Variable
object would result in its value being changed.
That's how it works, but |previous| is a property of the Variable
object here.

The op wants to not return previous, but to change the value
(possible), and have that value reflected outside (not possible). It
doesn't work that way.

But the code needs to be syntactially correct; It needs some parens
around the if condition - if (condition).
Javascript has no operators that are capable of modifying its primitive
values (so including strings and numbers). That renders the question moot.

In the case of your example a more practical strategy would be to take
advantage of the function's return value and have it return the item form
the list, if found, or null if not found. You can both test the return value
for trueness (elements, being objects, type-convert to true while null
type-converts to false) and end up with the value you are looking for, to do
with as you will.
Yep.

var previous = getPreviousElement(element, list);

But you could use:
var previous = el.previousSibling
 
T

Tom de Neef

Lasse Reichstein Nielsen said:
Parameters in Javascript is exclusively passed by value (i.e.,
function calls are call-by-value). You cannot pass a variable,
only its value, so there is no way a method can change a variable
that it doesn't have in its own scope. The variable "previous" at
the call point and the parameter "previous" of the function are
completely unrelated, whatever their names are.

There are ways around this, but none of them pretty.

The typical way to implement a function like your example is to have
it return the previous element if it exists, and return undefined
if it doesn't (or even throw an error, if not-existing shouldn't
happen). Returning values by assigning to reference-passed variables
is always dangerous - mostly because it makes code harder to read if
you can't easily see where a local variable is changed.


No, all parameters are passed by value, both object references and
primitive types. You can change the content of an object that you
are passed a reference to, but that's a different thing completely.

Thank you (and Richard and 'dhtml').
That is clear then. I am translating from Pascal to JavaScript. Mostly it is
easy to find what is possible and not so easy to find what is not possible.
Tom
 
R

Richard Cornford

dhtml said:
On Feb 17, 7:43 am, Richard Cornford wrote:

The Variable object's |previous| property gets a new value.

The Variable object doesn't get a new property.

Why have you just repeated what I wrote in a more vague form?
|previous| is a Reference, but a only of the Variable object.

A reference "of" a Variable object? I could understand a reference to a
Variable object (even if javascript cannot expose one) but that does not
make any sense.

The Variable object (the one associated with the particular call to the
function) has a property named "previous" that has a value. That value may
be a reference to another object or a primitive value, and that value may be
changed to a different value.
This is
a little more than the OP is asking for thouhg.

Other then asked for, not necessarily "more".
You mean that assigning to a
That's how it works, but |previous| is a property of the Variable
object here.

And "a property of an object referred to by the previous property" is not a
property of the Variable object (at the very least because the inability to
access a reference to a function call's Variable object would make it
impossible to assign that reference to the - previous - property of the
variable object. So what is your point here?
The op wants to not return previous, but to change the value
(possible), and have that value reflected outside (not possible).

That is what he said. What is your point here?
It doesn't work that way.

But the code needs to be syntactially correct; It needs some
parens around the if condition - if (condition).

I have already said that as well. I hope there is going to turn out to be
some point to your post.
Yep.

var previous = getPreviousElement(element, list);

But you could use:
Me?

var previous = el.previousSibling

I see no evidence here that the subject of interest is DOM nodes or that if
it were a - previousSibling - relationship would be one sought. This may be
a question purely of finding arbitrary elements in an array, particularly if
the interest includes string and numeric values.

Richard.
--
"In JavaScript, null, 0, '', false, and undefined are all equal (==) to each
other, since they all evaluate to false. This means that if you use the code
test == false, it will evaluate true if test is also undefined or equal to
null, which may not be what you want." - John Resig: Pro JavaScript
Techniques. 2006
 
D

Doug Gunnoe

A reference "of" a Variable object? I could understand a reference to a
Variable object (even if javascript cannot expose one) but that does not
make any sense.

I think he just means 'object reference'.
 
D

dhtml

Why have you just repeated what I wrote in a more vague form?


A reference "of" a Variable object? I could understand a reference to a
Variable object (even if javascript cannot expose one) but that does not
make any sense.
The Variable object has properties.

These include the parameters.

The properties are References.
The Variable object (the one associated with the particular call to the
function) has a property named "previous" that has a value. That value may
be a reference to another object or a primitive value, and that value may be
changed to a different value.

Correct.


Other then asked for, not necessarily "more".



And "a property of an object referred to by the previous property" is not a
property of the Variable object (at the very least because the inability to
access a reference to a function call's Variable object would make it
impossible to assign that reference to the - previous - property of the
variable object. So what is your point here?
when the identifier |previous| is found, on the Variable object, the
value on the rhs of the assignment sets the value of previous.

There's not creating a new property on the Variable object to replace
the old one; the Reference to |previous| gets a new value. There's a
clear, but subtle difference.

That is what he said. What is your point here?
Clarification.
Me?
This is getting silly.
I see no evidence here that the subject of interest is DOM nodes or that if
it were a - previousSibling - relationship would be one sought. This may be
a question purely of finding arbitrary elements in an array, particularly if
the interest includes string and numeric values.
That is true.
 
R

Richard Cornford

dhtml said:
The Variable object has properties.

These include the parameters.

The properties are References.

Properties of objects are not References (and if you use an uppercase 'R'
you may be taken as meaning the internal Reference type which they could
never be even in such a nebulous explanation as yours). Properties are
name/value pairs. The values _may_ be references to objects, but also may
not be.

So when you wrote "|previous| is a Reference, but a only of the Variable
object" you meant "|previous| is a property, but a only of the Variable
object", which is true but not news in context.
LOL.

when the identifier |previous| is found, on the Variable object, the
value on the rhs of the assignment sets the value of previous.

Not when you are setting "a property of an object referred to by the
previous property". In that case it is the property of the object referred
to that may be set to the value assigned.
There's not creating a new property on the Variable object to
replace the old one;

Did I ever say there was? I wrote "result in its value being changed" and
was not talking about a property of the Variable object being changed (but
rather "a property of an object referred to by the previous property" as I
wrote).
the Reference to |previous| gets a new value.

Not during an assignment to "a property of an object referred to by the
previous property".
There's a clear, but subtle difference.

There is a clear disconnection between what you are writing and what you are
writing it in response to.
Clarification.

You are not achieving it.
This is getting silly.

Editing quotes without marking the sections you edit is disingenuous at the
very least. You wrote "But you could use:" in a response to a post of mine,
implying the "you" in question was me. It is reasonable to question that
because in this context there is not even an implied suggestion that I would
use anything. And even if I did it would not be on the basis of advice from
you (you have quite a bit to learn about javascript yet before that would
happen).
That is true.

You see. Not only are there no grounds to expect me to use that, but there
are no grounds to expect the OP's doing that to even be relevant to his
problem.

Richard.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,223
Latest member
Jurgen2087

Latest Threads

Top