/    Sign up×
Community /Pin to ProfileBookmark

passing a collection as parameter

is it possible to pass a whole collection of [b]objects[/b] as a parameter?

function bla(){
var col = new Array()
..build the colection
bla2(col);
..continue with the new collection
col[0]… and so on….
}

function bla2(col){
…do something with the collection [b]col[/b] which becomes [b]newcol[/b]
this.col=newcol;
return this.col;
}

Or it is better to use a [b]new Function()[/b] to do that?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 18.2005 — [font=trebuchet ms]You can pass an array as a parameter, so I'm sure you can send an array that carries objects in it.[/font]
Copy linkTweet thisAlerts:
@JonaMar 18.2005 — [font=trebuchet ms]Here's an example.[/font]

<script type="text/javascript"><!--
var ary = [new Object(), new Object(), new Object()];
var props = [["red", "square", "blue"],
["blue", "circle", "red"],
["green", "rhombus", "pink"]];

function myFunc(){
var aryLen = ary.length;
for(var i=0; i<aryLen; i++){
ary[i] = ary[i].fillIn(props[i][0], props[i][1], props[i][2]);
alert("Object #"+i+" has the following properties:n"
+"Color: "+ary[i].color+"n"
+"Shape: "+ary[i].shape+"n"
+"Border color: "+ary[i].border);
}
}

Object.prototype.fillIn = function(color, shape, border){
this.color = color;
this.shape = shape;
this.border = border;
return this;
}

myFunc();
//--></script>
Copy linkTweet thisAlerts:
@KorauthorMar 18.2005 — well, I just wonder which way to start (to gain sime time) as I have noticed that a collection of objects are not exactly similar with arrays... So that I'll start building the function this "arrayed" way and hope it will work.

The ideea is to build a single function to "evaluate" from time to time the collection, as it is to be changed frequently on events (drag'n'drop app)
Copy linkTweet thisAlerts:
@Willy_DuittMar 18.2005 — You can try:

<i>
</i> function clone(collection){
for(var i in collection){
this[i] = collection[i];
} <br/>
} <br/>


.....Willy
Copy linkTweet thisAlerts:
@KorauthorMar 18.2005 — aha... That's a good ideea, thank you all. Let's see....?
Copy linkTweet thisAlerts:
@KorauthorMar 18.2005 — a... Willy... I guess I must be careful when operate some array's specific methods upon the new collection, it might not work... I'll test it anyway...
Copy linkTweet thisAlerts:
@Willy_DuittMar 18.2005 — Pass it as a new object...

<i>
</i> var props = [["red", "square", "blue"],
["blue", "circle", "red"],
["green", "rhombus", "pink"]];

function clone(collection){
for(var i in collection){
this[i] = collection[i];
} <br/>
}

var cloneProps = new clone(props);
alert(cloneProps(props[1]);


Not really sure what it is you are trying to do so that is as far as I can go... But, not long ago in response to sorting a multi-dimensional array, I posted a simular cloning routine which may help you...

.....Willy
Copy linkTweet thisAlerts:
@KorauthorMar 18.2005 — The problem is that i need to pass a [b]collection[/b] of [b]objects[/b] not a simple array of variables...
Copy linkTweet thisAlerts:
@Willy_DuittMar 18.2005 — [i]Originally posted by Kor [/i]

[B]The problem is that i need to pass a [b]collection[/b] of [b]objects[/b] not a simple array of variables... [/B][/QUOTE]


However you slice it...

A collection is still an array...

And a variable is still an object...

.....Willy
Copy linkTweet thisAlerts:
@Khalid_AliMar 18.2005 — well if I understand you correctly then take a look at the following explaination.

Everything in an html document is a collection/array of its children objects. which u should be able to pass to any function or you can put severl parents in an array and pass that array to a function, or you can create a tone of objects and put them in array and pass them to a function.

The point is JavaScript document model is extremely simple to work with. Explain your question with a bit of detail and see if you get a better response
×

Success!

Help @Kor 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...