-
-
Save modster/81f38ac96d3dbe908bda8d9616682f94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Dynamic Object properties using square brackets | |
*/ | |
let beverage = 'Beer'; | |
const myObj = { | |
a: 1, | |
b: 2, | |
c: 3, | |
food: 'cheese', | |
propName: 'cheese', | |
beverage: 'Heineken', // beverage: 'Heineken' | |
[beverage]: 'corona', // Beer: 'corona' | |
}; | |
let addProp = (obj, propName, propValue) => { | |
// obj.food = 'cheese'; | |
obj[propName] = propValue; | |
obj.propName = propValue; | |
}; | |
addProp(myObj, 'food', 'cheese'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment