Everything is possible, but his will require you to supply meta data on your
function when setting it, which is quite a lot of extra work when creating
your function. The current method uses Function.length, which is a built in
feature in JS - which meant no extra syntax for it to work. Your request
would have to be something like:
Object.overload(obj,'name',[
{ fnc : function(a,b,c){....}, args: [ 'sting','number','string' ] }
, .....
]);
this will also mean that every check will need to check for Function.length,
then iterate on arguments ( I guess serializing can make this simpler, but
it's still a lot of overhead)
IMO - imposing strict typing on a dynamic type language like JS is a bad
idea - aside from the overhead of type checking on every parameter every
time the function is called, this also introduces a lot of confusion for
people who use the Class, as no one expects his code to be strict typed.
I am however interested in making something like this, so I might take it up
for a weekend fun coding
On Sat, Mar 12, 2011 at 10:24 AM, Matthew Hazlett <[email protected]> wrote:
> Can this be extended to include type matching?
>
>
>
> Class is ‘debug’
>
> Overloaded function is ‘error’
>
>
>
> debug.error (1); // Looks up a number error code and writes a string
>
> debug.error (‘1’); // Dumps the string
>
>
>
> I know within the function ‘error’ you could do a typeOf, but can it be
> modified to intelligently decide what function to use based on the passed
> type?
>
>
>
> Something like:
>
> [ function error({a:’int’}){ //whatever }, function error([a:’str’}){
> //whatever } ]
>
>
>
> *From:* [email protected] [mailto:
> [email protected]] *On Behalf Of *???? ????
> *Sent:* Friday, March 11, 2011 5:50 AM
> *To:* [email protected]
> *Subject:* Re: [Moo] Re: Overloading method
>
>
>
> a small fix:
> http://jsfiddle.net/ariehg/ZDzPh/2/
> the overloader will now use the first function (or if overloaded after
> construction the original method) if no match was found.
>
> On Fri, Mar 11, 2011 at 12:48 PM, אריה גלזר <[email protected]>
> wrote:
>
> As promissed:
> http://jsfiddle.net/ariehg/ZDzPh/
>
> a note on usage - since Class wraps it's methods, it is impossible to know
> function's arguments length after construction. To avoid this limitation,
> simply override the methods on a literal object before passing it to the
> Class constructor (as shown in the demo). This is actually a useful way of
> writing classes as it allows editor like eclipse to outline your methods.
>
>
>
> On Thu, Mar 10, 2011 at 8:30 PM, אריה גלזר <[email protected]> wrote:
>
> Porting Resig's idea won't be very hard. It would require some monkey
> patching though. I might be able to upload something like it in a few hours
>
>
>
> On Thu, Mar 10, 2011 at 3:29 PM, remram <[email protected]> wrote:
>
> Hi Slik,
> That is exactly, what I have looked for. But It's no Mootools. I found
> also a solution from John Resig
> http://ejohn.org/blog/javascript-method-overloading/
> . But is still no Mootools ;) I thought with Mootools would be
> possible. Thank you anyway ;)
>
>
>
> --
>
> Arieh Glazer
> אריה גלזר
> 052-5348-561
> http://www.arieh.co.il
> http://www.link-wd.co.il
>
>
>
>
>
>
> --
>
> Arieh Glazer
> אריה גלזר
> 052-5348-561
> http://www.arieh.co.il
> http://www.link-wd.co.il
>
>
>
>
>
>
> --
>
> Arieh Glazer
> אריה גלזר
> 052-5348-561
> http://www.arieh.co.il
> http://www.link-wd.co.il
>
>
>
--
Arieh Glazer
אריה גלזר
052-5348-561
http://www.arieh.co.il
http://www.link-wd.co.il