473,326 Members | 2,173 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,326 software developers and data experts.

ParamArrays in JavaScript functions possible?

Can javascript functions have variable number of arguments - a concept
known as paramarrays?

Apr 18 '06 #1
15 1568
Water Cooler v2 wrote on 18 apr 2006 in comp.lang.javascript:
Can javascript functions have variable number of arguments
<script type="text/javascript">

function sum() {
var summing = 0;
for(var i = 0; i < sum.arguments.length; i++)
summing += sum.arguments[i];
return summing;
}

alert(sum(1,2,3,4))

</script>
a concept known as paramarrays?


A rose by any ...
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 18 '06 #2
On 18/04/2006 12:54, Evertjan. wrote:

[snip]
function sum() {
var summing = 0;
for(var i = 0; i < sum.arguments.length; i++)

^^^^
The arguments object is a local variable. Referencing it via the
function object is (as I recall) deprecated.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 18 '06 #3
Michael Winter wrote on 18 apr 2006 in comp.lang.javascript:
On 18/04/2006 12:54, Evertjan. wrote:

[snip]
function sum() {
var summing = 0;
for(var i = 0; i < sum.arguments.length; i++)

^^^^
The arguments object is a local variable. Referencing it via the
function object is (as I recall) deprecated.


Could you give a better alternative, please?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 18 '06 #4
Thanks, Evertjan. So, internally, functions are objects, just like in
python. Cool.

....name would smell just as sweet (or, something like that; from Romeo
& Juliet, right?). Thanks!

Apr 18 '06 #5


Evertjan. wrote:

The arguments object is a local variable. Referencing it via the
function object is (as I recall) deprecated.


Could you give a better alternative, please?


You can simply do e.g.
for (var i = 0; i < arguments.length; i++)
in any function to loop through the arguments passed in. Doing
functionName.arguments
instead of simply
arguments
is deprecated.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 18 '06 #6
Water Cooler v2 wrote on 18 apr 2006 in comp.lang.javascript:
Thanks, Evertjan. So, internally, functions are objects, just like in
python. Cool.
Objects? I do not think you should conclude this out of my words.
...name would smell just as sweet (or, something like that; from Romeo
& Juliet, right?). Thanks!


Please quote what you are replying to. This is not email but usenet.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers. <http://www.safalra.com/special/googlegroupsreply/>

It seems that sum.arguments is outdated and just arguments works fine.

==============================

However jscript 5.6 specs shows this:

function ArgTest(a, b){
var numargs = arguments.length; //Get number of arguments passed.
var expargs = ArgTest.length; //Get number of arguments expected.
[.....]

.... introducing ArgTest.length as the length of the specified/expected
arguments, new to me!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 18 '06 #7


Water Cooler v2 wrote:
So, internally, functions are objects, just like in
python.


Functions are objects in JavaScript, that is right, but certainly not
only "internally", whatever "internally" means exactly.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 18 '06 #8
Martin Honnen wrote on 18 apr 2006 in comp.lang.javascript:
Evertjan. wrote:
The arguments object is a local variable. Referencing it via the
function object is (as I recall) deprecated.

Could you give a better alternative, please?


You can simply do e.g.
for (var i = 0; i < arguments.length; i++)
in any function to loop through the arguments passed in. Doing
functionName.arguments
instead of simply
arguments
is deprecated.


Thanks, see my other post for functionName.length.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 18 '06 #9
>Please quote what you are replying to. This is not email but usenet.

huh?

a concept known as paramarrays?
A rose by any ...

Thanks, anyways.

Apr 18 '06 #10
Evertjan. wrote:
Michael Winter wrote on 18 apr 2006 in comp.lang.javascript:
for(var i = 0; i < sum.arguments.length; i++)

The arguments object is a local variable. Referencing it via the
function object is (as I recall) deprecated.

Could you give a better alternative, please?


Just "arguments.length"

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Apr 18 '06 #11
Water Cooler v2 wrote:
So, internally, functions are objects, just like in
python. Cool.


Don't assume that they are exactly like in Python: Javascript doesn't have
a similar concept to Python's bound methods so if you try to use a method
as a first class object you will be disappointed.
Apr 18 '06 #12
On 18/04/2006 13:29, Evertjan. wrote:

[snip]
However jscript 5.6 specs shows this:

function ArgTest(a, b){
var numargs = arguments.length; //Get number of arguments passed.
var expargs = ArgTest.length; //Get number of arguments expected.
[.....]

... introducing ArgTest.length as the length of the
specified/expected arguments, new to me!


It's specified in ECMA-262 (see 15.3.5.1, 3rd Ed.). The length property
of a function object is 'usually an integer that indicates the "typical"
number of arguments expected by the function.' For user-defined
functions, it is the number of formal arguments defined for the function
(see step 8 in 13.2 Creating Function Objects). For built-in and host
functions, it can vary.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 18 '06 #13
Michael Winter <m.******@blueyonder.co.uk> writes:
On 18/04/2006 13:29, Evertjan. wrote:
... introducing ArgTest.length as the length of the
specified/expected arguments, new to me!

It's specified in ECMA-262 (see 15.3.5.1, 3rd Ed.).


For completeness, it was in ECMA 262, 1st Ed too. Quoting:

15.3.5.1 length
The value of the length property is usually an integer that
indicates the "typical" number of arguments expected by the
function. However, the language permits the function to be
invoked with some other number of arguments. The behavior of
a function when invoked on a number of arguments other than
the number specified by its length property depends on the
function.

(Haven't checked if it's identical to 3rd Ed, but it probably is)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 18 '06 #14
On 18/04/2006 18:37, Lasse Reichstein Nielsen wrote:
Michael Winter <m.******@blueyonder.co.uk> writes:
On 18/04/2006 13:29, Evertjan. wrote:
... introducing ArgTest.length as the length of the
specified/expected arguments, new to me!
It's specified in ECMA-262 (see 15.3.5.1, 3rd Ed.).


For completeness, it was in ECMA 262, 1st Ed too.


I only mentioned the version explicitly in case the section number was
different.
Quoting:

15.3.5.1 length
[snip]
(Haven't checked if it's identical to 3rd Ed, but it probably is)


It's only different in that the 3rd Ed. adds:

This property has the attributes { DontDelete, ReadOnly,
DontEnum }.

to the end. It is identical to the 2nd Ed.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 18 '06 #15
JRS: In article <Em******************@text.news.blueyonder.co.uk >,
dated Tue, 18 Apr 2006 16:21:56 remote, seen in
news:comp.lang.javascript, Michael Winter <m.******@blueyonder.co.uk>
posted :

It's specified in ECMA-262 (see 15.3.5.1, 3rd Ed.). The length property
of a function object is 'usually an integer that indicates the "typical"
number of arguments expected by the function.'

True, but that's a bad specification.

I have a function
function ShoFFF() { // Args may be functions, but 0 ends visibles
with (ShoGen(arguments)) Depikt(BoxW, Cnt, Str, "lightgreen") }
Now ShoFFF.length gives 0 ; but the typical number of arguments is
greater than zero (indeed, ShoFFF seems to loop indefinitely with no
arguments, which needs explaining and [ideally] eliminating[*].)

The spec should probably say that it indicates, for user-defined
function, the number of arguments indicated in the definition, and ...

[*] Eliminated, anyway.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 18 '06 #16

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

Similar topics

0
by: Avis | last post by:
Hi, Iam trying to convert a VB6.0 class library in which I have implemented byref paramarrays for pulling the values back to the caller method: A sample method is given bellow: public...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.