I know that in JavaScript you can add new properties to an existing type (like Date), but is it possible to add new properties to an anonymous type after it's been defined?
For example, say I have the following script:
var employee = {
'Name': 'Scott',
'Age': 32,
'JavaScriptNewbie': true
};
Later on in my script I want to add a new property to this employee object (say, Salary). Is that possible?
TIA!
You can add new properties to an existing object by simply giving it a value.
One way is to add a property using the dot notation: obj. foo = 1; We added the foo property to the obj object above with value 1.
the type of this property cannot be changed between data property and accessor property, and. the property may not be deleted, and. other attributes of its descriptor cannot be changed (however, if it's a data descriptor with writable: true , the value can be changed, and writable can be changed to false ).
The most common and well-known way to keep an object from being changed is by using the const keyword. You can't assign the object to something else, unlike let and var , but you can still change the value of every property, delete a property, or create a property.
employee.Salary = value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With