In my JavaScript code, I have a class called Bank that has a method "addBranch" which adds a Branch object to an array of branches. My goal is to set up a custom attribute "customers" for each Branch object as it gets added to the array.
Below is a simplified snippet of the code:
class Bank {
#name
#branches
constructor(name) {
this.#name = name
this.#branches = []
}
// Name getter
get name() {
return this.#name
}
// Branches getter
get branches() {
return this.#branches
}
// Method to add branch
addBranch(branch) {
// Check if branch already exists
if (this.#branches.includes(branch)) return false
// Initialize customers attribute for the new branch
branch.customers = []
this.#branches.push(branch)
console.log(`Pushed new branch: ${branch}`)
return true
}
I attempted to use "branch.customers = []" but encountered this error:
TypeError: Cannot create property 'customers' on string 'Branch1'. at Bank.addBranch (C:\Users\npdkh\Desktop\Integrify\fs17-week2-TS\src\bank.js:25:22). at Object. (C:\Users\npdkh\Desktop\Integrify\fs17-week2-TS\src\bank.js:85:6).
Node.js v18.17.1