How to add a property to an object at runtime

S

scott.howe

Is there a way to add a property to a javascript object at runtime if
you don't the name until runtime?

For Example:

var oTestObj = new Object();
//In the real world the value of sPropertyName would be calculated.
var sPropertyName = "NewProperty";

//How do you add a property whos name is the value of sPropertyName?

oTestObj.NewProperty = "Somevalue";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top