How can I dynamically add attributes to a Javascript object/class?
I am parsing an xml file, for each name value pair in a xml element I am trying to add that pair as an attribute to a Javascript object. See my example for clarity:
function MyObject(nType)
{
this.type = nType;
}
MyObject.prototype.parseXMLNode( /*XML Node*/ nodeXML )
{
var attribs = nodeXML.attributes;
for (var i=0; i<attribs.length; i++)
this.(attribs[i].nodeName) = attribs[i].nodeValue; // ERROR here
}
// Where the xml would look like
<myobject name="blah" width="100" height="100"/>
You’re very close. To Dynamically call and assign attributes on an object you’ll want to use bracket notation.
For example:
The following should work for you: