>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 someCalculatedV alue = 'someValue';
anotherFunction (someCalculated Value, [[[all args passed into
firstFunction]]]);
}
I understand, that I can use....
anotherFunction .apply( this, firstFunction.a rguments);
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 1978
Bonzo wrote: I understand, that I can use....
anotherFunction .apply( this, firstFunction.a rguments);
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(firstFu nction.argument s));
On 21/11/2005 16:36, Duncan Booth wrote:
[snip] anotherFunction .apply( this, [extra1, extra2].concat(firstFu nction.argument s));
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(a rguments, extra1, extra2);
or:
/* Add arguments to rear */
Array.prototype .push.call(argu ments, 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.set Attribute('oncl ick', '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 addEventListene r/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.set Attribute('oncl ick', '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.onc lick = 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 addEventListene r/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")
addEventListene r 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.onc lick = 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.l ength; 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 defined in
"A.html", but every attempt results in an "is not a function" error. I
have tried to invoke the function using parent.document.funcname(),
top.document.funcname(), and various other identifying methods, but all
result in the above...
|
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 processing a loop of rows
and calling a stored proc for each row. All is encapsulated within a single
oracle transaction.
I have a logic class that is making a call to the dataaccess class and
passing the rows. I can serialize the dataobject...
|
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 Compact Framework.
public class Executor {
public static Execute(string p_whichFunction) {
// call the function named p_whichFunction
|
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 respond.
I tried calling similar dll for a function which take integer by
refernce. It works ok in this case.
Please can you let me know if we can call a c++ win32 dll function by
passing a xmldocument by reference?
| |
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 than the thread it was
created on."
But I am locking before I touch the shared data, so do I actually need to
worry about this, or is it just the debugger being oversimplistic?
Specifically, I have:
|
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,
Boston, MA" name="yourName" style="width: 287px" />
|
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...
|
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...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |