On Thu, Nov 29, 2007 at 04:00:26AM -0800, Tobie Langel wrote:
> Unfortunately, JavaScript doesn't let you use apply on constructors.
> You're out of luck on that one.

Correction: JavaScript doesn't let you use apply on constructors *easily*.

var MyClass = Class.create({
  initialize: function(num1, num2, num3) { /* do stuff */ }
  });

var myargs = [ 1, 2, 3 ];
var foo = {};
foo.__proto__ = MyClass.prototype;
MyClass.apply(foo, myargs);

Note that the order of those last two lines is very important. I think that
with Prototype classes (i.e. those created with Class.create), you can sort
of skip a step:

var foo = {};
foo.__proto__ = MyClass.prototype;
foo.initialize.apply(foo, myargs);

Note that this is based on slides from Dan Webb and that I have not
actually tested it.

I might argue that the function returned by Class.create() should not only
call initialize but should first set this.__proto__ to
arguments.callee.prototype, making this sort of thing even simpler. Does
this sound appealing to anyone? It's a one-line patch.

> Best,
> Tobie
--Greg

> On Nov 29, 11:20 am, "Gareth Evans" <[EMAIL PROTECTED]> wrote:
> > Well, I tried google and it was proving to be difficult cos all I turned up
> > was articles on passing arrays to functions.. and I was pretty sure it was
> > done in prototype so I figured it was a good place to ask.
> >
> > I like that term, help vampire :)
> >
> > I was trying to apply on the Date constructor though, and the parser didnt
> > seem to like me (the script just stopped executing on that line), so I will
> > play around a bit more and see if I did something dumb or if I need to add
> > an extra set of brackets or something..
> > It's quite good to have a mailing list such as this which the occasional
> > idea can be bounced off other members...
> >
> > I know it's mostly prototype related but I have noticed an upturn in overall
> > questions recently..
> >
> > Google is usually my first port of call anyway, followed by asking people :)
> >
> > Gareth
> >
> > On 11/29/07, Thomas Fuchs <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > Slacker! I guess others will profit as well from this info. But don't
> > > you become a Help Vampire[1]. ;)
> >
> > > - Thomas
> > > [1]http://www.slash7.com/articles/2006/12/22/vampires
> >
> > > Am 29.11.2007 um 09:22 schrieb Gareth Evans:
> >
> > > > Thanks Thomas, I thought it was used in prototype but a lot of the
> > > > argument stuff I don't follow. It was quicker to ask :)
> >
> > > > Gareth
> >
> > > > On Nov 29, 2007 9:21 PM, Thomas Fuchs < [EMAIL PROTECTED]> wrote:
> >
> > > > You want Function#apply.
> >
> > > > see
> > >http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Gl...
> >
> > > > Best,
> > > > Thomas
> >
> > > > Am 29.11.2007 um 07:39 schrieb Gareth Evans:
> >
> > > > > Hey Guys
> >
> > > > > Does anyone know if I have an array if I can pass that array to a
> > > > > function, *as* the parameters.. not pass it as a parameter, but
> > > > > effectively have my scoped array become function.arguments .
> > > > > I have a regular expression that splits an ISO date string that's
> > > > > given me a match array, which I then split to drop the first match
> > > > > (the whole string) leaving me with year,month,day,hour,min,sec and I
> > > > > want to pass that to new Date()
> > > > > I could go var d = new Date(ma[0],ma[1],ma[2]... but I figure if I
> > > > > can do the arguments thing then its a technique i could adopt
> > > > > elsewhere.
> > > > > I control both sides of the interface, the source as well as the
> > > > > processing so I know the format will always be the same.
> > > > > (incidentally, the date comes from a .net date originally, and I use
> > > > > a .tostring("s") to get the iso format, which is passed to json as a
> > > > > string - there may be a better way for that)
> >
> > > > > Hope this makes sense,
> >
> > > > > Gareth
> > 
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to