Re: functions named the same by Anthony
Anthony
Wed Dec 05 02:13:01 PST 2007
"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:4755de14$0$17525$9b4e6d93@newsspool4.arcor-online.net...
> Robert schrieb:
>
> > I see what you mean and that sounds very plausible... offers an
intriguing
> > possibility as well... I am sure I can get better than this but you can
"kind
> > of" override a function like this....
> >
> > on error resume next
> >
> > Dim stringtorun
> > stringtorun = "msgbox testme(8,5,4)"
> >
> > executeglobal stringtorun
> >
> > if err.number <> 0 then
> > executeglobal "function testme(int1,int2,int3) : testme = int1 +
int2 +
> > int3: end function"
> > executeglobal stringtorun
> > End if
> >
> > on error goto 0
> >
> > function testme (int1,int2)
> > testme = int1 - int2
> > end function
> >
> > In this code the function with two parameters is called but if an error
is
> > generated (wrong number of parameters) then the same code is called
again,
> > this time it works taking the function definition added using the
execute
> > global command,,,
> >
> > I wonder if I could get some kind of generic override function to work?
I
> > will have a go...
>
> Use JScript and pass in any number of arguments you like.
>
> function addIt()
> {
> var a = arguments, i = a.length, r = 0;
> while (i--)
> r += isNaN(a[i]) ? 0 : a[i];
> return r;
> }
>
> WSH.Echo(addIt(1,2,3,4,5) * addIt(1,2));
>
>
> Also JScript provides real means for dynamically creating new
> function definitons on runtime with its Function-object:
>
> var divide = new Function ("dividend", "divisor",
> "return (divisor == 0 ? null : dividend/divisor);"
> );
>
> WSH.Echo (divide(10,3));
>
>
> and also for removing the function:
>
> divide = undefined;
>
>
> So you can also temporarily override it:
>
> function math() {
> var a = arguments, i = a.length, r = 0;
> while (i--) r += isNaN(a[i]) ? 0 : a[i];
> return r;
> }
>
> WSH.Echo(math(1,2,3,4,5));
> var mathOld = math;
>
> math = new Function(
> "var a = arguments, i = a.length, r = 1;"
> + "while (i--) r *= isNaN(a[i]) ? 1 : a[i];return r;"
> );
>
>
> WSH.Echo(math(1,2,3,4,5));
> math = mathOld;
>
> WSH.Echo(math (1,2,3,4,5));
>
Note though if the code for the function is known at design time then its
better to avoid instancing a function with a string. Instead use:-
var divide = function (dividend, divisor) {
return (divisor == 0 ? null : dividend/divisor);
}
OR
math = function() {
var a = arguments, i = a.length, r = 1;
while (i--) r *= isNaN(a[i]) ? 1 : a[i];return r;
}
That way the parser can flag up syntax errors when the file is loaded
instead of waiting until the function gets instanced. It will also be
quicker since the parser need only process the function body once.
I think its a design flaw to define a named function then override it with a
different implementation. It would be better to use a variable to start
with so that it is clear that the actual function that gets executed may
vary.
--
Anthony Jones - MVP ASP/ASP.NET