Forums > Javascript

adding properties to an object on the fly

November 14, 2006 | 12:47 pm

hi,
i’m trying to add arbitrary properties to an object by sending
messages from max.
while the messagename should end up as the property name of the object,
the message arguments should be assigned to the new property as an
array.
e.g.:
max message: paramA 3 2 5 9

in js: myObject.paramA = [3, 2, 5, 9];

right now i’m using the anything function to add the properties to my
object
and an array to store the names of the newly created properties,
in order to be able to retrieve the data easily.
see file below.

this works ok. but the code looks a little copious to me,
especially that i have to convert the messagename to a string.
is this the way to go?
is there something more "elegant"?

thanks to anybody who is willing to take a look at the code.
volker.

// start of js file — save as addProps.js

var container = new Object();
var names = new Array();

function anything() {
container[ messagename.toString() ] = arrayfromargs( arguments );
names.push( messagename );
}

// recall the data, all properties synchronized, one value
function msg_int( index ) {
for( j=0; j
outlet( 0, names[ j ], container[ names[ j ].toString() ] [ index ] );
}
}
// end of file

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P comment 73 73 120 196617 recall;
#P number 75 94 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 75 179 32 196617 print;
#P message 124 112 80 196617 paramB 5 7 3 1;
#P message 123 94 81 196617 paramA 3 4 2 1;
#P newex 75 147 75 196617 js addProps.js;
#P comment 124 73 120 196617 store some sequences;
#P connect 1 0 4 0;
#P connect 2 0 1 0;
#P connect 3 0 1 0;
#P connect 5 0 1 0;
#P window clipboard copycount 7;


November 14, 2006 | 5:21 pm

Your for statement looks a bit messed up..

Mattijs


November 14, 2006 | 7:47 pm

>
> Your for statement looks a bit messed up..
>
> Mattijs

could you please elaborate a little on the above statement? like
this it’s not all-too useful.
do you mean, messed up, in the sense of ‘not working properly’?
well, it does what i had in mind, so yes, it is working properly.
that’s why i included the patch,
to demonstrate how it’s supposed to function.
if you mean, it’s working, but could be improved, then yes, that’s
what i already stated.
the question remaining is how?

i don’t see the culprit in the for statement itself, but more in the
way i try to retrieve data from
an object’s properties that i have created ‘on the fly’.

what makes the code look even worse is that the values i’m assigning
to the properties are themselves arrays.
and i don’t want to retrieve the complete arrays but only a certain
index, which i specify with an integer
message to the js object. but that’s not the point i’m wondering about.
volker.



jml
November 16, 2006 | 2:16 am

the for statement (as it was delivered in your post) is not complete.
you probably want something like (email coding):

function msg_int(index){
for(j=0;j
outlet(0,names[j],container[names[j].toString()][index]);
}
}

jl



jml
November 16, 2006 | 4:46 am

the only thing i would mention about "elegance" is that you don’t have to explicitly change your name elements into strings.
js is more forgiving than that.

jl


November 16, 2006 | 11:51 am

> the for statement (as it was delivered in your post) is not complete.
> you probably want something like (email coding):
>
> function msg_int(index){
> for(j=0;j
> outlet(0,names[j],container[names[j].toString()][index]);
> }
> }

yes, that’s what i posted originally and that’s what it looks like in
the mail i received from the list.
just checked on the forum and indeed, there it’s crippled… sorry
for that, wonder why that is.
v


November 16, 2006 | 11:54 am

On 16 Nov 2006, at 05:46, jLubow wrote:

>
> the only thing i would mention about "elegance" is that you don’t
> have to explicitly change your name elements into strings.
> js is more forgiving than that.
>
> jl

ja, that works. thanks for the hint!
volker.


Viewing 7 posts - 1 through 7 (of 7 total)