PDA

View Full Version : How to add an attribute to an object dynamically



raulooo
21 Apr 2010, 7:56 AM
how do i accomplish this?


var dataRecord = {
'prop1':'valueprop1'
}

dataRecord.addAttribute('prop2','valueprop2'); // Is there any method like this in ExtJS or JS

so that i can do this

for(var i; i<atr.length;i++){
dataRecord.addAttribute(atr[i],val[i]); // or something
}



I couldn't do this with Ext.apply.

thanks in advance.

Jaitsu
21 Apr 2010, 8:32 AM
if you want to add an "attribute" (property) to an object in JS



var data = {
property: 'property1'
}

data.propertyTwo = 'propertyTwo';

mschwartz
21 Apr 2010, 8:43 AM
or:
data['propertyTwo'] = 'whatever';

CrazyEnigma
21 Apr 2010, 9:57 AM
Or


Ext.apply(data, {prop2: 'valueprop2'});

raulooo
21 Apr 2010, 10:58 AM
Thanks all of u guys for the fast replies

I think this do what i want,


or:
data['propertyTwo'] = 'whatever';

Because I want to add properties in a string way, and not like literals, so that i can do the foor loop i mentioned in my first post.

I've actually "solved" this in other way using Ext.data.Record and then setting prop/vals with the set method, but this is better because i can add dynamically to any hash object (because hash object is how is that called, right? if so why hash?).

thanks everyone.