Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing a variable number of arguments into the a constructor beingused with the `new' keyword

Reply
Thread Tools

Passing a variable number of arguments into the a constructor beingused with the `new' keyword

 
 
Andrey Fedorov
Guest
Posts: n/a
 
      12-10-2008
Is it possible?

Example:

foo(1,2,3); /* === */ foo.apply(window, [1,2,3]);

// but

new Foo(1,2,3); /* === */ ???
 
Reply With Quote
 
 
 
 
Martin Rinehart
Guest
Posts: n/a
 
      12-11-2008
On Dec 10, 5:59*pm, Andrey Fedorov <afedor...@gmail.com> wrote:
> Is it possible?
>
> Example:
>
> foo(1,2,3); * */* === */ * *foo.apply(window, [1,2,3]);
>
> // but
>
> new Foo(1,2,3); * */* === */ * *???


new Foo( [1,2,3] ); // one argument, any length
 
Reply With Quote
 
 
 
 
Andrey Fedorov
Guest
Posts: n/a
 
      12-11-2008
> To properly set up the prototype you need a little more [...]

This is what I'm doing now. My problem is that then, the `constructor'
property still isn't set correctly and hence the `instanceof' operator
doesn't work. One solution I'm considering is:

function Foo(arg1, arg2, ...) {
if (!arg.length) return; // required
// rest of code
}

function new_obj(type, args) {
x = new type();
type.apply(x, [1,2,3]);
return x;
}

x = new_obj(Foo, [1,2,3]);

But I'd love to be able to avoid that starting line inside of Foo.

Martin Rinehart wrote:
> new Foo( [1,2,3] ); // one argument, any length


I don't know if you're serious, but this is actually where I'm
leaning, if I don't figure out an elegant way of doing the former.

Cheers,
Andrey
 
Reply With Quote
 
Henry
Guest
Posts: n/a
 
      12-11-2008
On Dec 11, 4:09 pm, Andrey Fedorov wrote:
> ... . My problem is that then, the `constructor'
> property still isn't set correctly


If you don't like the way a 'constructor' property is set then why not
set it the way you want it?

> and hence the `instanceof' operator
> doesn't work. ...


The - instanceof - operator does not employ, and so has no interest
in, the - constructor - properties of objects. The instance of
operator makes an assertion about the runtime relationship between the
value assigned to the - prototype - property of a function and the
objects on the prototype chain of an object (produces a true result if
the function's prototype property value at that time is the same
object as one of the objects on the prototype chain of the operand
object).
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      12-11-2008
Andrey Fedorov <> writes:

> Is it possible?
>
> Example:
>
> foo(1,2,3); /* === */ foo.apply(window, [1,2,3]);
>
> // but
>
> new Foo(1,2,3); /* === */ ???


No.

What you can do is to make Foo handle an array argument properly:

function Foo(yadda, yadda2) {
if (arguments.length == 1 && yadda instanceof Array) {
Foo.apply(this, yadda);
return this;
}
}

I.e., calling the constructor as a function on the new object using
apply.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-11-2008
Andrey Fedorov wrote:
> Is it possible?


Is *what* possible? <http://jibbering.com/faq/#posting>

> Example:
>
> foo(1,2,3); /* === */ foo.apply(window, [1,2,3]);
>
> // but
>
> new Foo(1,2,3); /* === */ ???


IIUC, short of modifying Foo() to accept an Array object reference,
IMHO there is a good chance that the following worked:

var f = new Foo();
Foo.apply(f, [1, 2, 3]);

(Examples where it doesn't work this way include Date() since per
specification it ignores all arguments when [[Call]]ed rather than
[[Construct]]ed.)

Is this only a theoretical question or do you have an actual case
where you need it?


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-11-2008
Andrey Fedorov wrote:
>> To properly set up the prototype you need a little more [...]

>
> This is what I'm doing now. My problem is that then, the `constructor'
> property still isn't set correctly and hence the `instanceof' operator
> doesn't work. One solution I'm considering is:
>
> function Foo(arg1, arg2, ...) {
> if (!arg.length) return; // required


Probably you mean

if (!arguments.length) return; // required

> // rest of code
> }
>
> function new_obj(type, args) {
> x = new type();

^ ^^^^
Using `type' as an identifier is a bad idea, it might become a reserved
word. Not declaring `x' a local variable is worse, especially here:

> type.apply(x, [1,2,3]);

^
> return x;

^
> }
>
> x = new_obj(Foo, [1,2,3]);

^ ^
|
Identifiers for constructors should start uppercase.

> But I'd love to be able to avoid that starting line inside of Foo.


Why would you want to avoid using the gauntlet? Why waste resources going
through initialization when it is doomed to fail? (Note that a constructor
call will return an object reference no matter the return value.)

Please include an attribution line for each quotation level you leave in:

<http://jibbering.com/faq/#posting>


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-11-2008
Thomas 'PointedEars' Lahn wrote:
> Andrey Fedorov wrote:
>> function new_obj(type, args) {
>> x = new type();

> ^ ^^^^
> Using `type' as an identifier is a bad idea, it might become a reserved
> word. Not declaring `x' a local variable is worse, especially here:
>
>> type.apply(x, [1,2,3]);

> ^
>> return x;

> ^
>> }
>>
>> x = new_obj(Foo, [1,2,3]);

> ^ ^
> |
> Identifiers for constructors should start uppercase.


new_obj() is not [[Constructed]] here, it serves as a factory. But the
recommendation would definitely apply to `type', and changing that
accordingly would address two potential problems in one step.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
Keyword arguments in a block, possible with zero arguments? Peter Motzfeldt Ruby 1 03-13-2007 01:33 PM
Windows error message "Cannot remove file or folder. It is beingused by another person or program" SchoolTech NZ Computing 4 04-10-2006 05:44 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57