Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
How to access a property inside an object and use in another method? javascript

I need to create a method to return a customers name and bank account value like so:

*bank.printAccount('Sheldon'); // this should print 'Sheldon account is 0'*

However I am struggling to find out how to access the property customers to use in a method printAccount. Currently I have this:

class Bank {
    constructor(){
        this.customers = {};
    }
  
  }
  Bank.prototype.addCustomer = function(customer){
       this.customers[customer] = 0;

  }
  Bank.prototype.printAccount = function(){
  

  };
  var bank = new Bank();
  bank.addCustomer('Sheldon');
  bank.addCustomer('Bob');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There you go:

class Bank {
  constructor() {
      this.customers = {};
  }
}
Bank.prototype.addCustomer = function(customer) {
     this.customers[customer] = 0;
}
Bank.prototype.printAccount = function(customer) {
     console.log(this.customers[customer])
};

var bank = new Bank();
bank.addCustomer('Sheldon');
bank.addCustomer('Bob');
bank.printAccount('Sheldon');
bank.printAccount('Bob');

You probably will need to add edge cases in printAccount() such as to check whether the customer exists or not.

Edit: Removed doubled initialization of customers.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!