Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add properties to an anonymous JavaScript type after defined?

Tags:

javascript

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!

like image 720
Scott Mitchell Avatar asked Sep 15 '10 17:09

Scott Mitchell


People also ask

Can a property be added to an existing object in JavaScript?

You can add new properties to an existing object by simply giving it a value.

How do you add properties to an object?

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.

Can we define an object in JavaScript which property can not be changed?

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 ).

How do you set one object property that Cannot be changed or modified?

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.


1 Answers

employee.Salary = value;
like image 119
Quentin Avatar answered Nov 15 '22 14:11

Quentin