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

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

var dataRecord = {

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.

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';

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

21 Apr 2010, 9:57 AM

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

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

I think this do what i want,

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.