473,698 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function and arguments as aguments

Hi. I have the following code:

--------------------------------------
function Tunnel() {
//arguments[0](???);
}

function Sum() {
var sum = 0;
for (i=0; i<arguments.len gth; i++) sum += arguments[i];
alert(sum);
};

// calling Sum directly
Sum(1,2,3,4);

// calling Sum through Tunnel
Tunnel(Sum, 1,2,3,4);
--------------------------------------

How should the Tunnel function be defined, knowing that the number of
parameters passed are unknown?

telmo
Feb 15 '06
41 2565
Thomas 'PointedEars' Lahn wrote:
[...]
Since `arguments' inherits directly from Object (ECMASCript 3 Final,
10.1.8), it is possible to add them, e.g.:

Object.prototyp e.join = function(delim)
{
var a = [];

for (var i = 0, len = this.length; i < len; i++)
{
a.push(this[i]);
}

return a.join(delim);
}

[...]

But since Array.prototype .join is generic (under ECMASCript 3), it
seems you should be able to use the built-in here, given that
'arguments': has a length and "numeric" properties:

Object.prototyp e.join = Array.prototype .join;

../rh

Feb 16 '06 #11
ro********@gmai l.com wrote:
Thomas 'PointedEars' Lahn wrote:
Since `arguments' inherits directly from Object (ECMASCript 3 Final,
10.1.8), it is possible to add them, e.g.:

Object.prototyp e.join = function(delim)
{
var a = [];

for (var i = 0, len = this.length; i < len; i++)
{
a.push(this[i]);
}

return a.join(delim);
}


But since Array.prototype .join is generic (under ECMASCript 3), it
seems you should be able to use the built-in here, given that
'arguments': has a length and "numeric" properties:

Object.prototyp e.join = Array.prototype .join;


Indeed. Thank you for pointing this out. When reading only

,-[ECMAScript Edition 3 Final]
|
| 15.4.4.5 Array.prototype .join (separator)
|
| The elements of the array are converted to strings, [...]
^^^^^^^^^^^^^^^ ^^^^^^^^^^

it is easy to overlook the following at the top of the next page
(PDF version):

| NOTE The join function is intentionally generic; it does not require that
| its `this' value be an Array object. Therefore, it can be transferred to
| other kinds of objects for use as a method. Whether the join function
| can be applied successfully to a host object is implementation-dependent.

(It should be noted *g* that there is such a NOTE for other methods of
Array.prototype too.)

The test case

Object.prototyp e.join = Array.prototype .join;
(function() { alert(arguments .join("x")); })(1, 2, 3); // "1x2x3"
alert({1: 2, 2: 3, length: 3}.join("x")); // "x2x3"

works in the following user agents:

- Mozilla Firefox 1.5.0.1
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060209
Debian/1.5.dfsg+1.5.0. 1-2 Firefox/1.5.0.1

- Mozilla (Suite) 1.7.12
[Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20060205
Debian/1.7.12-1.1]

- Netscape Navigator 4.8
[Mozilla/4.8 [en] (X11; U; Linux 2.6.15.1-20060130.184242 +0100 i686)]

- Opera 8.51
[Opera/8.51 (X11; Linux i686; U; en)]

- Konqueror 3.5.1
[Mozilla/5.0 (compatible; Konqueror/3.5;
Linux 2.6.15.1-20060130.184242 +0100; X11; i686; de, en_US)
KHTML/3.5.1 (like Gecko) (Debian package 4:3.5.1-2)]

IE, Windows, and Mac testers, it's your turn :)
PointedEars
Feb 16 '06 #12
Thomas 'PointedEars' Lahn wrote:
RobG wrote:

UnaCoder wrote:
Well I thought there was and I researched java script to see if you can
invoke a string containing java script code. the solution that seemed
intuitive would be to do something like:

function Tunnel() {
function_name= arguments[0];
arguments.shift (); args = arguments.join( );
eval function_name + "(" + args + ");";
}

I didn't put any error checking in there, or test that code, but as far
as I know that should work =)


Arguments is not an array, it's just a list


s/list/(Object) object/


Yes, a list Object.

with a few other properties that make it look a bit like an array.
Consequentl y, it doesn't have split() or join() methods (and I don't
think you can add them to its prototype).


Since `arguments' inherits directly from Object (ECMASCript 3 Final,
10.1.8), it is possible to add them, e.g.:


The way I read the spec was that arguments shares the Object prototype,
which is null. I concluded that you couldn't add to it - clearly an
incorrect conclusion. :-x

I'll run the test in your other post later on a few Mac OS browsers.
--
Rob
Feb 17 '06 #13
VK

Thomas 'PointedEars' Lahn wrote:
ro********@gmai l.com wrote:
Thomas 'PointedEars' Lahn wrote:
Since `arguments' inherits directly from Object (ECMASCript 3 Final,
10.1.8), it is possible to add them, e.g.:

Object.prototyp e.join = function(delim)
{
var a = [];

for (var i = 0, len = this.length; i < len; i++)
{
a.push(this[i]);
}

return a.join(delim);
}


But since Array.prototype .join is generic (under ECMASCript 3), it
seems you should be able to use the built-in here, given that
'arguments': has a length and "numeric" properties:

Object.prototyp e.join = Array.prototype .join;


Indeed. Thank you for pointing this out. When reading only

,-[ECMAScript Edition 3 Final]
|
| 15.4.4.5 Array.prototype .join (separator)
|
| The elements of the array are converted to strings, [...]
^^^^^^^^^^^^^^^ ^^^^^^^^^^

it is easy to overlook the following at the top of the next page
(PDF version):

| NOTE The join function is intentionally generic; it does not require that
| its `this' value be an Array object. Therefore, it can be transferred to
| other kinds of objects for use as a method. Whether the join function
| can be applied successfully to a host object is implementation-dependent.

(It should be noted *g* that there is such a NOTE for other methods of
Array.prototype too.)

The test case

Object.prototyp e.join = Array.prototype .join;
(function() { alert(arguments .join("x")); })(1, 2, 3); // "1x2x3"
alert({1: 2, 2: 3, length: 3}.join("x")); // "x2x3"

works in the following user agents:

- Mozilla Firefox 1.5.0.1
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060209
Debian/1.5.dfsg+1.5.0. 1-2 Firefox/1.5.0.1

- Mozilla (Suite) 1.7.12
[Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20060205
Debian/1.7.12-1.1]

- Netscape Navigator 4.8
[Mozilla/4.8 [en] (X11; U; Linux 2.6.15.1-20060130.184242 +0100 i686)]

- Opera 8.51
[Opera/8.51 (X11; Linux i686; U; en)]

- Konqueror 3.5.1
[Mozilla/5.0 (compatible; Konqueror/3.5;
Linux 2.6.15.1-20060130.184242 +0100; X11; i686; de, en_US)
KHTML/3.5.1 (like Gecko) (Debian package 4:3.5.1-2)]

IE, Windows, and Mac testers, it's your turn :)


It works in JScript 5.6 (IE 6.0)

For the purity of picture :-) one should say that in JScript
"arguments" is an objects with non-enumerable properties "length",
"callee", "caller" and "00"..."0X"
Besides that "00"..."0N of arguments" properties are *protected* so you
can access them only by numeric index, so say arguments[1] is really an
access to the property arguments['01'] which is not accessible
otherwise. Overall it looks like HTMLCollection where scring property
access is locked. That is the reason why it is stated in docs that
"arguments is not an array despite it looks like one" (because it's
indeed not).

Feb 17 '06 #14
Thomas 'PointedEars' Lahn wrote:
The test case

Object.prototyp e.join = Array.prototype .join;
(function() { alert(arguments .join("x")); })(1, 2, 3); // "1x2x3"
alert({1: 2, 2: 3, length: 3}.join("x")); // "x2x3"

works in the following user agents:

- Mozilla Firefox 1.5.0.1
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060209
Debian/1.5.dfsg+1.5.0. 1-2 Firefox/1.5.0.1

- Mozilla (Suite) 1.7.12
[Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20060205
Debian/1.7.12-1.1]

- Netscape Navigator 4.8
[Mozilla/4.8 [en] (X11; U; Linux 2.6.15.1-20060130.184242 +0100 i686)]

- Opera 8.51
[Opera/8.51 (X11; Linux i686; U; en)]

- Konqueror 3.5.1
[Mozilla/5.0 (compatible; Konqueror/3.5;
Linux 2.6.15.1-20060130.184242 +0100; X11; i686; de, en_US)
KHTML/3.5.1 (like Gecko) (Debian package 4:3.5.1-2)]

IE, Windows, and Mac testers, it's your turn :)


It appears to also work on IE 5.5 and IE 6.
Feb 17 '06 #15
RobG wrote:
Thomas 'PointedEars' Lahn wrote:
RobG wrote:
UnaCoder wrote:
Well I thought there was and I researched java script to see if you can
invoke a string containing java script code. the solution that seemed
intuitive would be to do something like:

function Tunnel() {
function_name= arguments[0];
arguments.shift (); args = arguments.join( );
eval function_name + "(" + args + ");";
}

I didn't put any error checking in there, or test that code, but
as far as I know that should work =)
Arguments is not an array, it's just a list s/list/(Object) object/


Yes, a list Object.


There is no such thing.
with a few other properties that make it look a bit like an array.
Consequently, it doesn't have split() or join() methods (and I don't
think you can add them to its prototype).

Since `arguments' inherits directly from Object (ECMASCript 3 Final,
10.1.8), it is possible to add them, e.g.:


The way I read the spec was that arguments shares the Object prototype,


You misunderstood it.

| 10.1.8 Arguments Object
| [...]
| * The value of the internal [[Prototype]] property of the arguments object
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
| is the original Object prototype object, the one that is the initial
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
| value of Object.prototyp e (section 15.2.3.1).

That means essentially that `arguments' is an Object object, as it
inherits directly from Object.prototyp e through the prototype chain.
Which makes it possible to augment Object.prototyp e and call its
methods with `arguments' as caller.

And since there are is no constraint for `arguments' regarding this,
it is possible, as with any Object object, to augment it directly.
Since it has been pointed out that Array.prototype .join and other
methods of Array.prototype are generic, direct augmentation is
indeed of practical value:

function x()
{
arguments.join = Array.prototype .join;
alert(arguments .join(","));
}

Only the current `arguments' object would be affected by it then,
not other Object objects. (Think about iterating through their
properties with `for..in'.)
which is null. [...]
No, fortunately it is not :) There would not be such nice properties
as `constructor' and methods as toString() otherwise. You are confusing
the prototype object and its [[Prototype]] property:

| 15.2.3.1 Object.prototyp e
|
| The initial value of Object.prototyp e is the Object prototype object
| (section 15.2.4).
|
| This property has the attributes { DontEnum, DontDelete, ReadOnly }.

The last attribute means essentially that you cannot overwrite the value of
Object.prototyp e with a reference to another object as that would break the
prototype chain mechanism preserved by the following:

| 15.2.4 Properties of the Object Prototype Object
|
| The value of the internal [[Prototype]] property of the Object prototype
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
| object is null [...]

Hopefully this helps (you) to see the difference:

arguments.[[Prototype]] --> Object.prototyp e
Object.prototyp e --> {constructor: ..., toString: ..., ...}
Object.prototyp e.[[Prototype]] --> null
I'll run the test in your other post later on a few Mac OS browsers.


Thanks in advance.
Regards,
PointedEars
Feb 17 '06 #16
VK wrote:
Thomas 'PointedEars' Lahn wrote:
The test case

Object.prototyp e.join = Array.prototype .join;
(function() { alert(arguments .join("x")); })(1, 2, 3); // "1x2x3"
alert({1: 2, 2: 3, length: 3}.join("x")); // "x2x3"

works in the following user agents:

- Mozilla Firefox 1.5.0.1
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.1) Gecko/20060209
Debian/1.5.dfsg+1.5.0. 1-2 Firefox/1.5.0.1

- Mozilla (Suite) 1.7.12
[Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20060205
Debian/1.7.12-1.1]

- Netscape Navigator 4.8
[Mozilla/4.8 [en] (X11; U; Linux 2.6.15.1-20060130.184242 +0100 i686)]

- Opera 8.51
[Opera/8.51 (X11; Linux i686; U; en)]

- Konqueror 3.5.1
[Mozilla/5.0 (compatible; Konqueror/3.5;
Linux 2.6.15.1-20060130.184242 +0100; X11; i686; de, en_US)
KHTML/3.5.1 (like Gecko) (Debian package 4:3.5.1-2)]

IE, Windows, and Mac testers, it's your turn :)
It works in JScript 5.6 (IE 6.0)


OK, thanks.
For the purity of picture :-) one should say that in JScript
"arguments" is an objects with non-enumerable properties "length",
"callee", "caller"
That is ECMAScript-compliant, except of the `caller' property. It is
identical to JavaScript before version 1.3. Since that version, the
latter property is only a property of the called Function object;
otherwise the implementation is identical.
and "00"..."0X"
I seriously doubt that.
Besides that "00"..."0N of arguments" properties are
protected so you can access them only by numeric index
How did you get /that/ idea anyway?
so say arguments[1] is really an access to the property arguments['01']
Most certainly it is not.
which is not accessible otherwise.
Because it does not exist. The property name is '1', not '01', as all
property names are strings and number-value names provided in bracket
property accessors are converted to string before the actual property
access occurs.
Overall it looks like HTMLCollection where scring property access
is locked.
It does not look like an HTMLCollection object at all. There is no
namedItem() and no item() method, and removing one element does not
change following key-value relationships.

"String property access" (whatever that might be) is not locked with
HTMLCollection objects. It is only that the type of the argument to
the property accessor defines which one of the methods, namedItem()
[for strings] or item() [for numbers] is actually called.

Try

arguments.join = Array.prototype .join;
alert(arguments .join(","));

If that works in function code, `arguments' is not "locked" in any way.
I am confident that this applies. So I dare say, without having tested
it in IE, assigning 42 to arguments['01'] will most certainly get you 42
for arguments['01'] but of course not for arguments['1'] or arguments[1]
in JScript, too. Because those are _different_ properties.
That is the reason why it is stated in docs that "arguments is not
an array despite it looks like one" (because it's indeed not).


That is not the reason why. The reason is that it does show certain
features of Array-ness, such as properties with numeric name and a `length'
property, but lacks others, such as the join() and split() methods. Even
with an Array object referred to by identifier `a', assigning to a['01']
would not add an element to the array.

ISTM that your problem is that you have still not understood Array objects,
and property accesses in general.

BTW, you could have trimmed your quotes.
PointedEars
Feb 17 '06 #17
VK

Thomas 'PointedEars' Lahn wrote:
Besides that "00"..."0N of arguments" properties are
protected so you can access them only by numeric index
How did you get /that/ idea anyway?


JScript 5.6 Language Reference

"arguments Object"

<quote>
You cannot explicitly create an arguments object. The arguments object
only becomes available when a function begins execution. The arguments
object of the function is not an array, but the individual arguments
are accessed the same way array elements are accessed. The index n is
actually a reference to one of the 0n properties of the arguments
object.
</quote>
ISTM that your problem is that you have still not understood Array objects,
and property accesses in general.


It is mostly your problem as you still did not get that ECMAScript
specs is a technical description of an engine which is gone years ago
(NN 4.5) so the next generations could reproduce its *behavior* if they
decide so. The *real* internal mechanics mimicing a particular behavior
may have nothing in common (and often it hasn't) with the mechanics of
NN as it was in 1999.

IE does it this way, FF this way, someone else may in other way. It is
not really important *most of the time* as long as the top level
behavior seems "as spelled".

Feb 17 '06 #18
VK wrote:
Thomas 'PointedEars' Lahn wrote:
> Besides that "00"..."0N of arguments" properties are
> protected so you can access them only by numeric index

How did you get /that/ idea anyway?


JScript 5.6 Language Reference

"arguments Object"

<quote>
You cannot explicitly create an arguments object. The arguments object
only becomes available when a function begins execution. The arguments
object of the function is not an array, but the individual arguments
are accessed the same way array elements are accessed. The index n is
actually a reference to one of the 0n properties of the arguments
object.
</quote>


Given your posting history, you should not even try to patronize me. This
is *obviously* a typo. It should read "0...n", for "0 to n", because it
goes on with:

| [...]
| See Also
|
| Reference
|
| 0...n Properties (JScript 5.6)
| [...]

Do I have anything more to say?
ISTM that your problem is that you have still not understood Array
objects, and property accesses in general.


It is mostly your problem as you still did not get that ECMAScript
specs is a technical description of an engine which is gone years
ago (NN 4.5) [snipped further nonsense]


Hear, hear!
PointedEars
Feb 17 '06 #19
On 17/02/2006 18:42, Thomas 'PointedEars' Lahn wrote:

[snip]
| 10.1.8 Arguments Object
| [...]
| * The value of the internal [[Prototype]] property of the
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
| arguments object is the original Object prototype object,
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
| the one that is the initial value of Object.prototyp e
| (section 15.2.3.1).
As I've always understood the 'original Object prototype' phrase, and it
seems rather explicit, above, the arguments object may only ever have
the properties as described in 15.2.3.1. That is, because it's
[[Prototype]] property is the /original/ Object prototype object,
modifications to that prototype will not be seen with an arguments
object. The fact that some implementations do use a modified Object
prototype object is a peculiarity of those implementations .

Note that the only other use of the phrase, 'original Object prototype
object', is found in the description of the [[Construct]] property
(13.2.2) where the original Object prototype object will be used if the
prototype property of the Function object is not an object.
That means essentially that `arguments' is an Object object, as it
inherits directly from Object.prototyp e through the prototype chain.
Which makes it possible to augment Object.prototyp e and call its
methods with `arguments' as caller.


Assuming for the moment that I'm correct, that would make this statement
false: it /shouldn't/ be possible (except through extensions to the
language) to use the properties of an augmented Object prototype object.
So, whilst:

arguments.join = Array.prototype .join;
arguments.join( );

and:

Array.prototype .join.call(argu ments);

would be acceptable:

Object.prototyp e.join = Array.prototype .join;
arguments.join( );

is dubious.

[snip]

A final, related note: the arguments object /is/ an Array instance in Opera.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 17 '06 #20

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

Similar topics

25
9123
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
7
23547
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.