problem with arguments and class creation

0 views
Skip to first unread message

Davide

unread,
Dec 18, 2009, 8:16:43 AM12/18/09
to Prototype & script.aculo.us
Hi all,

I have a class defined like this

var C = Class.create({
initialize:function() {
alert("ext: " + arguments.length)
this.b(arguments);
},
b:function() {
alert("int: " + arguments.length)
}
});

when I create the class using :

c= new C("aa","aa","aa","aa")

I have the arguments count is 4 in the initialize part and it's 1 in
the b function. Output is

ext: 4
int: 1

Is there a way to pass the arguments from ext to int in a consistent
way ?

Frédéric

unread,
Dec 18, 2009, 8:27:43 AM12/18/09
to prototype-s...@googlegroups.com
Le vendredi 18 décembre 2009 13:16, Davide a écrit :

> var C = Class.create({
>    initialize:function() {
>      alert("ext: " + arguments.length)
>       this.b(arguments);
>    },
>     b:function() {
>       alert("int: " + arguments.length)
>     }
> });

Try:

var C = Class.create({
initialize:function() {
alert("ext: " + arguments.length)

b.apply(this, arguments); // <<<<<<<<<


},
b:function() {
alert("int: " + arguments.length)
}
});


--
Frédéric

Davide

unread,
Dec 18, 2009, 8:34:57 AM12/18/09
to Prototype & script.aculo.us
Hi

I played a bit and your solution is giving me
INT:2

I implemented this now.

var C = Class.create({
initialize:function() {
alert("ext: " + arguments.length)

b.apply(arguments);
},
b:function() {
alert("int: " + arguments[0].length) ;<<<<<<<<<<<<<<<<<<<
}
});

which is giving correct INT / EXT values.

It seems to me a bit overcomplicated.. is this the right way ?

Can you please explain what would do the 'this' as you were
suggesting ? I guess that there is something I am missing in the big
picture...

Frédéric

unread,
Dec 18, 2009, 8:47:31 AM12/18/09
to prototype-s...@googlegroups.com
Le vendredi 18 décembre 2009 13:34, Davide a écrit :

> I played a bit and your solution is giving me
> INT:2
>
> I implemented this now.
>
> var C = Class.create({
> initialize:function() {
> alert("ext: " + arguments.length)
> b.apply(arguments);
> },
> b:function() {
> alert("int: " + arguments[0].length) ;<<<<<<<<<<<<<<<<<<<
> }
> });
>
> which is giving correct INT / EXT values.
>
> It seems to me a bit overcomplicated.. is this the right way ?
>
> Can you please explain what would do the 'this' as you were
> suggesting ? I guess that there is something I am missing in the big
> picture...

I'm new to Prototype, so I'll let gurus explain the details ;o)
This is the solution I got for my Signal class, some days ago (see
T.J. Crowder answer on 07/12/2009 for my post named 'Custom signal').

--
Frédéric

T.J. Crowder

unread,
Dec 18, 2009, 10:35:45 AM12/18/09
to Prototype & script.aculo.us
Hi,

Frédéric was just missing out the `this` keyword:

var C = Class.create({
initialize: function() {
alert("ext: " + arguments.length)

this.b.apply(this, arguments);
},

b: function() {
alert("int: " + arguments.length)
}
});

Function#apply[1] and its counterpart Function#call[2] are two of the
more powerful features of JavaScript (not Prototype). the only
difference between them is how you pass in the arguments to the
function being called (as an array-like-thing [#apply] or individually
[#call]).

[1] https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Apply
[2] https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Function/Call

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Dec 18, 1:16 pm, Davide <daw...@gmail.com> wrote:
> Hi all,
>
> I have a class defined like this
>

> var C = Class.create({
> initialize:function() {
> alert("ext: " + arguments.length)
> this.b(arguments);
> },
> b:function() {
> alert("int: " + arguments.length)
> }
>
> });
>

> when I create the class using :
>
> c= new C("aa","aa","aa","aa")
>
> I have the arguments count is 4 in the initialize part and it's 1 in
> the b function. Output is
>
> ext: 4
> int: 1
>
> Is there a way to pass the arguments from ext to int in a consistent
> way ?

Frédéric

unread,
Dec 18, 2009, 10:41:29 AM12/18/09
to prototype-s...@googlegroups.com
Le vendredi 18 décembre 2009 15:35, T.J. Crowder a écrit :

> Frédéric was just missing out the `this` keyword:

Oups, right! Sorry...

--
Frédéric

Davide

unread,
Dec 18, 2009, 1:43:17 PM12/18/09
to Prototype & script.aculo.us
Cool Guys !

Thank you all,

implemented and working perfectly !

Also good for increasing JS culture !

Reply all
Reply to author
Forward
0 new messages