js set object property code example
Example 1: how to add property to object in javascript
var data = {
'PropertyA': 1,
'PropertyB': 2,
'PropertyC': 3
};
data["PropertyD"] = 4;
// dialog box with 4 in it
alert(data.PropertyD);
alert(data["PropertyD"]);
Example 2: javascript add to object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
// Array of Objects in form {element: {id: 10, quantity: 10} }
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Example 3: js create object with properties
var mycar = new Car('Eagle', 'Talon TSi', 1993);
Example 4: javascript object string property
// Also useful for dynamic strings, e.g. `thing-${variable}`
myObject['thing'] = true;
Example 5: js create object with properties
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
Example 6: how to change object property value in javascript
myObject.sProp = 'A new string value for our original string property';