Many arguments...

C

CBlair1986

Hey all. I did a quick look-see, and I can't tell if there's a quicker
way to do what I want to do.

I'm using prototype.js, so if this is the wrong place to ask, sorry,
but I think it's just a really quick question.

In my script, I'm making something like:

var Foo = Class.create()

Foo.prototype = {
initialize: function( one, two, three, four, ..., twenty-seventh ) {
this.one = one;
this.two = two;
...
this.twenty-seventh = twenty-seventh;
}
}

Now, I'm going to do this a lot (approximately thirty or forty times),
so I was wondering if there was a way to use the arguments array and a
little bit of evaluation tomfoolery to make it a bit more compact.

Thanks for any insights you give!
 
C

CBlair1986

Hey all. I did a quick look-see, and I can't tell if there's a quicker
way to do what I want to do.

I'm using prototype.js, so if this is the wrong place to ask, sorry,
but I think it's just a really quick question.

In my script, I'm making something like:

var Foo = Class.create()

Foo.prototype = {
initialize: function( one, two, three, four, ..., twenty-seventh ) {
this.one = one;
this.two = two;
...
this.twenty-seventh = twenty-seventh;
}

}

Now, I'm going to do this a lot (approximately thirty or forty times),
so I was wondering if there was a way to use the arguments array and a
little bit of evaluation tomfoolery to make it a bit more compact.

Thanks for any insights you give!

Hehe, hate to reply to myself, but I figured out that javascript
*does* n fact have an 'eval' function. I put this together really
quickly, and it does use prototype, so it might not be so useful, but
it works for me.

function makeFunc( args ) { // args is an array of strings
str = "function( " + args.join(", ") + " ) {\n"
args.each( function( arg ) {
str = str + "this." + arg + " = " + arg + ";\n"
}
str = str + "}";
return str;
}

And to use it:

eval(makeFunc( $w("one two three four ... foo")));

Hope this might help someone other than me, too!
 
E

Evertjan.

CBlair1986 wrote on 25 mrt 2007 in comp.lang.javascript:
Hehe, hate to reply to myself, but I figured out that javascript
*does* n fact have an 'eval' function. I put this together really
quickly, and it does use prototype, so it might not be so useful, but
it works for me.

function makeFunc( args ) { // args is an array of strings
str = "function( " + args.join(", ") + " ) {\n"
args.each( function( arg ) {
str = str + "this." + arg + " = " + arg + ";\n"
}
str = str + "}";
return str;
}

And to use it:

eval(makeFunc( $w("one two three four ... foo")));

Hope this might help someone other than me, too!

Did it help you? Did you test it? I doubt it!

Where do you reference this $w?

There is no reason here to use eval() and eval is evil!
 
T

TheBagbournes

CBlair1986 said:
Hey all. I did a quick look-see, and I can't tell if there's a quicker
way to do what I want to do.

I'm using prototype.js, so if this is the wrong place to ask, sorry,
but I think it's just a really quick question.

In my script, I'm making something like:

var Foo = Class.create()

Foo.prototype = {
initialize: function( one, two, three, four, ..., twenty-seventh ) {
this.one = one;
this.two = two;
...
this.twenty-seventh = twenty-seventh;
}
}

Now, I'm going to do this a lot (approximately thirty or forty times),
so I was wondering if there was a way to use the arguments array and a
little bit of evaluation tomfoolery to make it a bit more compact.

The arguments to a function are already an array.

(Just ignore the shouting, dogmatic "eval is evil" chants - what they really mean is "eval is mostly unecessary")

Try

Foo.prototype = {
initialize: function() {
for (var i = 0; i < arguments.length; i++)
{
alert(arguments);
}
}
};

But be prepared to press return a lot if you pass 27 args!
 
D

Douglas Crockford

CBlair1986 said:
In my script, I'm making something like:

var Foo = Class.create()

Foo.prototype = {
initialize: function( one, two, three, four, ..., twenty-seventh ) {
this.one = one;
this.two = two;
...
this.twenty-seventh = twenty-seventh;
}
}

Now, I'm going to do this a lot (approximately thirty or forty times),
so I was wondering if there was a way to use the arguments array and a
little bit of evaluation tomfoolery to make it a bit more compact.

A function with 27 ordered parameters is going to be unusable. You might
consider instead a function with one parameter: An object.

foo.initialize({one: 1, twenty_seventh: 27, three: 3});

Your function can then loop over its parameter.

initialize: function (param) {
var n;
for (n in param) {
if (param.hasOwnProperty(n)) {
this[n] = param[n];
]
}
}

http://javascript.crockford.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,340
Latest member
orhankaya

Latest Threads

Top