adding properties to an object on the fly


    Nov 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

    • Nov 14 2006 | 5:21 pm
      Your for statement looks a bit messed up..
      Mattijs
    • Nov 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.
    • Nov 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
    • Nov 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
    • Nov 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
    • Nov 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.