I have stumbled on a weird issue when trying to recursively set properties on an empty object with the following code:
Simplified code
const birthdays = {};
// Loop -> Passing day, id and birthday
birthdays[day] = day;
birthdays[day][id] = birthday;
Example of day: '01012017'
Example of id: 1547
Example of birthday: {name: John}
Error message
Cannot create property '123' on string '06012017'
I saw some people with Angular having this issue but their answer don't solve anything for me (as is angular specific syntax etc).
birthdays[day]with the day itself, using thebirthdays[day] = {}fixed it for me.birthdays['01012017'] = '01012017'and then thisbirthdays['01012017'][1547] = {name: 'John'}, and it will NOT trigger any such error.birthdays['01012017'][1547] === undefinedlet sDay='01012017'; birthdays[sDay]=sDay;then ⚡sDay.id='birthday'causes the TypeError as scalar system types (String, Number, Boolean,...) in general are "frozen" (can not have any new props assigned).Object.isFrozen('01012017') === true. To avoid this but keep the "string" feeling - a wrapper class could be used:class Value{constructor(v){this.value=v;}toString(){return this.value;}}makingbirthdays[day]=new Value(day);birthdays[day][1547]={name:"John"}is error free andbirthdays[day]has props andString(birthdays[day]) === '01012017'