/    Sign up×
Community /Pin to ProfileBookmark

looping ‘this’ assignments from passed variables

I’m trying to find a way (if there is one) of shortening a type of code that I use a lot, when building arrays. Some functions end up with a _lot_ of ‘this’ assigments of a functions arguments (example below). I’d like to automate this in some way, and i’ve tried a bunch of things. Including some stuff I’m not too familiar with, so I might have gone at it the wrong way.

Here’s what the code typically looks like:

function item(code,name,p11,p12,p13,p14,p21,p22,p23,p24,p31,p32,p33,p34,p41,p42,p43,p44,p51,p52,p53,p54) {
this.code = code;
this.name = name;
this.p11 = p11;
this.p12 = p12;
this.p13 = p13;
this.p14 = p14;
this.p21 = p21;
this.p22 = p22;
this.p23 = p23;
this.p24 = p24;
this.p31 = p31;
this.p32 = p32;
this.p33 = p33;
this.p34 = p34;
this.p41 = p41;
this.p42 = p42;
this.p43 = p43;
this.p44 = p44;
this.p51 = p51;
this.p52 = p52;
this.p53 = p53;
this.p54 = p54;
}

itemArray = new Array();
itemArray[0] = new item(‘ab680′,’Absolut Vodka’,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);

This is a fairly standard type of code that I’ve seen everywhere. Thing is, in some functions I’d like to use 50 or more arguments, and it would make the code much cleaner if there was a way of looping this, or shortening it up some other way.. I haven’t seen a reference to this anywhere, so either it’s impossible, or noone has cared enough yet.

This isn’t pivotal to me in any way, I’d just like to know if there is a way to to it..

/JP

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@GollumMay 16.2003 — You could just use the Javascript object literal syntax...

itemArray[0] =

{

code:'ab680',name:'Absolut Vodka',

p11:1,p12:2,p13:3,p14:4,

p21:5,p22:6,p23:7,p24:8,

p31:9,p32:10,p33:11,p34:12,

p41:13,p42:14,p43:15,p44:16,

p51:17,p52:18,p53:19,p54:20

};
Copy linkTweet thisAlerts:
@CharlesMay 16.2003 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<script type="text/javascript">

<!--

function Item (code, name) {

this.code = code;

this.name = name;

for (j=2; j<arguments.length; j++) {this['p' + j] = arguments[j]}

}

Item.prototype.toString = function () {

var a = new Array();

for (key in this) {a.push(key + '=' + this[key])};

return a.toString();

}

// -->

document.write (new Item('fee', 'fie', 1, 2, 3, 4, 5))

</script>[/font]
Copy linkTweet thisAlerts:
@rlmjinxauthorMay 16.2003 — Thanks for the replies!

I found Gollums suggestion interesting, but I can't use that in the context of what I'm working on at the moment. I'll keep it in mind though.

Charles, you gave me exactly what I'd been looking for.. Here's my revised 'item' function:

function item() {

itemvars = "code,name,p11,p12,p13,p14,p21,p22,p23,p24,p31

,p32,p33,p34,p41,p42,p43,p44,p51,p52,p53,p54"

itemvarsAr = itemvars.split(',');

for (j=0; j<arguments.length; j++) this[itemvarsAr[j]] = arguments[j];

}

Works like a charm..
Copy linkTweet thisAlerts:
@CharlesMay 16.2003 — [font=georgia]I've made a few improvements to your version.

1) I've followed the Java naming conventions as JavaScript does. (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html)

2) I've used an array literal to eliminate a lot of work for interpreter.

3) I've added checking in case you've passed too many parameters.[/font]

[font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<script type="text/javascript">

<!--

function Item() {

var vars = ['code','name','p11','p12','p13','p14','p21','p22','p23','p24','p31'

,'p32','p33','p34','p41','p42','p43','p44','p51','p52','p53','p54'];

for (j=0; j<(arguments.length > vars.length ? vars.length : arguments.length); j++) this[vars[j]] = arguments[j];

}


Item.prototype.toString = function () {

var a = new Array();

for (key in this) {a.push(key + '=' + this[key])};

return a.toString();

}

// -->

document.write (new Item('fee', 'fie', 1, 2, 3, 4, 5))

</script>[/font]
×

Success!

Help @rlmjinx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...