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! 5 1183
On Mar 25, 1:15 am, "CBlair1986" <CBlair1...@gmail.comwrote:
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!
CBlair1986 wrote on 25 mrt 2007 in comp.lang.javascript :
On Mar 25, 1:15 am, "CBlair1986" <CBlair1...@gmail.comwrote:
>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!
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!
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
CBlair1986 wrote:
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[i]);
}
}
};
But be prepared to press return a lot if you pass 27 args!
CBlair1986 wrote:
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/
TheBagbournes <no***@noway.comwrote:
The arguments to a function are already an array.
No. The arguments to a function are already an object which has some
similar behaviour to an array, but they are not an array. This does
actually matter in some situations, e.g. if you want to insert/delete
arguments and then forward it to another function.
(Just ignore the shouting, dogmatic "eval is evil" chants - what they
really mean is "eval is mostly unecessary")
And more specifically, in this particular case eval is completely
unnecessary and, as the OP's own followup demonstrates simply encourages
poor coding.
Douglas Crockford's suggestion is the best way, but even if the OP wants to
stick with unmanageable argument lists a simple for loop will do the job:
Foo.prototype = { initialize: function (one, two, three, four,
five, six) {
var argnames = /\(([^)]*)\)/.exec(this.initialize)[1].split(/,\s*/);
for (var i=0; i < argnames.length; i++) {
this[argnames[i]] = arguments[i];
}
}
}; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kenneth Brody |
last post by:
I know that passing printf() too few arguments, or arguments of the wrong
type invokes UB. However, what about passing too many arguments, if the
expected arguments are of the correct type?
For...
|
by: jn |
last post by:
Hi,
I'm passing around 30 arguments to a stored procedure, and
I got the following error (see below).
It works fine if I narrow down to around 25 args. Is there
any workaround to bypass this...
|
by: Martin Jørgensen |
last post by:
Hi,
Problem:
========
Some of my output functions are beginning to take pretty many
arguments... I mean.... We're talking about 10-15 arguments :-)
So I thought to myself, perhaps this is...
|
by: Alan G Isaac |
last post by:
My class MyClass reuses many default parameters
with a small number of changes in each instance.
For various reasons I decided to put all the
parameters in a separate Params class, instances
of...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |