如何动态地将属性添加到 Javascript 对象/类?
我正在解析一个 xml 文件,对于 xml 元素中的每个名称值对,我试图将该对作为属性添加到 Javascript 对象。为了清楚起见,请参阅我的示例:
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"/>