computed object property names code example
Example: Computed Property names
///////////////// COMPUTED PROPERTY NAMES ///////////////////
// allow you to pass in variables for use as property names
// when initializing an object
// hit alt / command J and type into console
// 1. declare a variable = "store a string inside it"
// variable string
const myProperty = "string";
// 2. then we will declare an object = { and give it a property }
// 3. For this we use our previous myProperty
// declare myObject = {
[put variable in square braces]: and "give it a value"
}
const myObject = {
[myProperty]: "This is my value"
}
// 4. Now check it look inside myObject
// you can see the proprty identifier was
// created using the string stored in myProperty
// call myObject
type
myObject (hit return result should be..)
{string: "This is my value"}
// You will sometimes create some code that
// will not know in advance what the property names will be
// and will create them from variables passed to it like this.