>From within a function, I want to pass a/some parameters to another
function, AND all arguments, passed into this function.
e.g.
function firstFunction(){
//this function may have been passed, 0, 1, or n arguments...
...
//call another function...
var someCalculatedValue = 'someValue';
anotherFunction(someCalculatedValue, [[[all args passed into
firstFunction]]]);
}
I understand, that I can use....
anotherFunction.apply( this, firstFunction.arguments);
to "pass" along ALL the arguments to the second function, but I CAN'T
seem to figure out, how to ALSO, pass other arguments (preferably as
the first args)
Cheers,
Bonzo 7 1926
Bonzo wrote: I understand, that I can use....
anotherFunction.apply( this, firstFunction.arguments);
to "pass" along ALL the arguments to the second function, but I CAN'T seem to figure out, how to ALSO, pass other arguments (preferably as the first args)
anotherFunction.apply( this,
[extra1, extra2].concat(firstFunction.arguments));
On 21/11/2005 16:36, Duncan Booth wrote:
[snip] anotherFunction.apply( this, [extra1, extra2].concat(firstFunction.arguments));
As I recall, the Array.prototype.concat method only adds numeric
properties one-by-one if the object is an Array. The arguments object is
not an array, so it will be appended as-is:
[extra1, extra2, arguments]
Both Firefox and IE display this behaviour, though surprisingly Opera
does not[1].
The more correct approach would be to use the Array.prototype.push or
unshift methods to add the extra arguments to the arguments object, then
pass that to the apply method (which can take either an array or an
arguments object):
/* Add arguments to front */
Array.prototype.unshift.call(arguments, extra1, extra2);
or:
/* Add arguments to rear */
Array.prototype.push.call(arguments, extra1, extra2);
then:
anotherFunction.apply(this, arguments);
Notice that the arguments object should not be qualified as a property
of a function object.
It should be pointed out that JScript versions prior to 5.5 don't
implement the apply or call methods, neither do they implement the
unshift or push methods. All four can be emulated though, if necessary.
I also wonder about this approach in general. It seems rather odd that a
function should want to pass through arguments in this manner. Perhaps
an alternative design could avoid the scenario entirely.
Mike
[1] On further examination, the arguments object is actually an
Array object in Opera.
--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Duncan & Michael,
Thanks for the input... I will definitely check this option out...
I understand your concern over the design Michael, it is a rather
strange one (my design that is)
It evolves from the desire to build elements via the DOM, then add
eventHandlers to them. (This has already been done, and works like
magic in all browsers except IE)
In Moz, and Opera, I can use:
someElement.setAttribute('onclick', 'funcB(event, ' + param1 + ', ' +
param2 + ');');
This will create the attribute, register the event, and all is well...
of course, IE will fail horribly... it will set the attribute, but not
register the event.
So, option2, was to call addEventListener/attachEvent, to add the event
handling... the only problem being, that I can't pass params (which,
argue as one might, I simply need for the "magic" I'm cultivating...
read: "seriously dynamically dynamic HTML")
So, I hacked** together something, that will wrap the "event
registration" call, to register the events in Moz, Opera, IE, etc.
A-N-D, allow me to pass parameters!...
**In the hack, I want to pass the event (default first argument in Moz,
Opera, etc.) to my handler, BUT, also (for simplicity), also pass it
along to IE (cause it is plain anoying to have to retrieve it inside
every single event handler on my page, if I can pass it instead)
***(there is actually more, but I'm trying to simplify here)
Long Story short, I have the event, and I have the parameters, and thus
I want to call the event handling function, with the event, and all
arguments passed to my "event handler wrapper"... on to my actual event
handler...
</end-scary-js-idea>
Alternatively, I've been told I might have some luck, iterating over
the arguments, and building up a "function string" that I can eval()
when complete... I'm not sure if this will solve my problem either, but
it seems the "hackiest" of the ideas I've seen thus far.
PS for anyone that is thinking it, no, I can't just stuff in
foo.innerHTML = ''; Although this will work in IE, it is not where I
want, nor care to go.
Cheers,
Bonzo
Bonzo wrote: Duncan & Michael,
Thanks for the input... I will definitely check this option out...
I understand your concern over the design Michael, it is a rather strange one (my design that is)
It evolves from the desire to build elements via the DOM, then add eventHandlers to them. (This has already been done, and works like magic in all browsers except IE)
In Moz, and Opera, I can use:
someElement.setAttribute('onclick', 'funcB(event, ' + param1 + ', ' + param2 + ');');
This will create the attribute, register the event, and all is well... of course, IE will fail horribly... it will set the attribute, but not register the event.
Have you considered using:
var parm1 = 'Parameter 1';
var parm2 = 'Parameter 2';
someElement.onclick = function(event){
funcB(event, parm1, parm2);
}
// ...
function funcB(e, p1, p2){
var e = e || window.event;
alert(e.type + '\n' + p1 + '\n' + p2);
}
Be careful with closures - they may or may not be what you are after,
they can be avoided if not. So, option2, was to call addEventListener/attachEvent, to add the event handling... the only problem being, that I can't pass params (which, argue as one might, I simply need for the "magic" I'm cultivating... read: "seriously dynamically dynamic HTML")
addEventListener is handy if you want to add multiple events, or don't
want to stomp on those that might already be there, but the above is
simpler and may suit better. So, I hacked** together something, that will wrap the "event registration" call, to register the events in Moz, Opera, IE, etc. A-N-D, allow me to pass parameters!...
If you post a small demo of what you are trying to do, life may be simpler. **In the hack, I want to pass the event (default first argument in Moz, Opera, etc.) to my handler, BUT, also (for simplicity), also pass it along to IE (cause it is plain anoying to have to retrieve it inside every single event handler on my page, if I can pass it instead)
You can't (AKAIK).
IE has a different event model, you have to get window.event from the
called function, so each such function has to have at least:
function funcB(e)
{
var e = e || window.event;
}
or some similar approach. The alternative is to call the function with
something like:
funcB(event, window.event);
But then funcB has to be something like:
function funcB(eMoz, eIE)
{
var e = eMoz || eIE;
}
and you're back where you started.
***(there is actually more, but I'm trying to simplify here)
Long Story short, I have the event, and I have the parameters, and thus I want to call the event handling function, with the event, and all arguments passed to my "event handler wrapper"... on to my actual event handler...
Then do as suggested above. An event handler wrapper can be used to
prevent closures, but otherwise shouldn't be necessary. </end-scary-js-idea>
Alternatively, I've been told I might have some luck, iterating over the arguments, and building up a "function string" that I can eval() when complete... I'm not sure if this will solve my problem either, but it seems the "hackiest" of the ideas I've seen thus far.
Yech, it may not be *the* 'hackiest', but certainly getting there. PS for anyone that is thinking it, no, I can't just stuff in foo.innerHTML = ''; Although this will work in IE, it is not where I want, nor care to go.
You are right not try it - it will likely fail some of the time in all
browsers for some elements and all of the time in others.
--
Rob
Michael Winter wrote: As I recall, the Array.prototype.concat method only adds numeric properties one-by-one if the object is an Array. The arguments object is not an array, so it will be appended as-is:
[extra1, extra2, arguments]
Both Firefox and IE display this behaviour, though surprisingly Opera does not[1].
You recall correctly. I was misled by the Mozilla Javascript reference:
"arguments
An array corresponding to the arguments passed to a function"
In fact the ecmascript specification clearly defines 'arguments' as
something which behaves mostly like an array but which isn't, and specifies
as you said that the concat method checks whether its argument is an array.
If Opera behaves differently that is clearly a bug.
Ok, chalk that one down to another case of 'never ever post until you've
checked your answer on running code', I'll crawl back to the python
newsgroup where duck typing rules.
Rob,
yeah, unfortunately I have thought of that... the trick I have, is that
I don't know if I will be passing 0, 1, 2, or n params...
I'll hack it a bit more, I'm sure I can come up with something that
will work. ;-)
Thanks for all the input folks!
Cheers,
Bonzo
Bonzo wrote: Rob,
yeah, unfortunately I have thought of that... the trick I have, is that I don't know if I will be passing 0, 1, 2, or n params...
Please quote what you are replying to, trim the excess. I guess you are
referring to:
someElement.onclick = function(event){
funcB(event, parm1, parm2);
}
// ...
function funcB(e, p1, p2){
var e = e || window.event;
alert(e.type + '\n' + p1 + '\n' + p2);
}
In the above, both IE and Geko (W3C?) event models 'event' will be
passed from the onclick as the first parameter. For Geko, 'e' will be a
reference to the event, in IE it will be undefined, hence it is given a
value using window.event. More explicitly, you could use:
if (typeof e == 'undefined' && window.event) {
var e = window.event;
} else {
// Deal with an event model we don't understand
}
But the original is shorter. In both cases, if you don't know how many
arguments funcB will get, use the arguments collection to get the rest
of the arguments from arguments[1] onward:
function funcB(e)
{
var e = e || window.event;
var arg;
for (var i=1, num=arguments.length; i<num; ++i){
arg = arguments[i];
// Do something with each arg, if there are any;
}
}
[...]
--
Rob This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Hammond |
last post by:
In document "A.html" I have defined a function and within the document
body have included an IFRAME element who's source is document "B.html".
In document "B.html" I am trying to call the function...
|
by: hasho |
last post by:
Why is "call by address" faster than "call by value"?
|
by: Jack Addington |
last post by:
I am working on app that currently all resides on the same machine but plan
to pull the database off and try to move all the datafunctionality to a
remote machine. I have a data function that is...
|
by: Joel Finkel |
last post by:
Is there a way to execute a method if all we know is its name as a string?
Let's say we have the following class. What is the code for the Execute
method? I need a solution that works with the...
|
by: saish |
last post by:
Hello
Need your help, I have a C++ win32 dll which takes xml document type by
reference. I need to call this dll from c# .net.
I tried using DllImport, but dll funtion when call does not...
|
by: priyasmita_guha |
last post by:
C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?
|
by: Dave Booker |
last post by:
I have a Thread making a callback to a Form function that alters Form data.
Of course, the debugger is telling me, "Cross-thread operation not valid:
Control 'FormTest' accessed from a thread other...
|
by: ravi |
last post by:
Hi,
i am a c++ programmer,
now i want to learn programming in c also.
so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
|
by: moni |
last post by:
Hi..
I am trying to use javascript for google maps display.
If I call the javascript function from my aspx file I use:
<input type="text" id="addresstext" value="Huntington Avenue,...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| | |