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

180
Views
Cómo acceder a métodos desde otra clase y crear objetos

¡Nuevo en programación orientada a objetos Javascript! Tengo 2 clases, Pedido y Cliente. Tengo un método getName(). Quiero al cliente como una de mis propiedades en la clase Pedido. He creado objetos para ambos, pero parece que no puedo acceder al nombre del cliente en el objeto del pedido.

Cliente.js

 class Customer{ constructor(id, name, address,creditCard) { this._id=id; this._name=name; this._address=address; this._creditCard=creditCard; } getName(){ return this._name; } }

Orden.js

 class Order { customer; constructor(id, date) { this._id = id; this._date=date; this.customer=new Customer(); } getNameCustomer() { return this.customer.getName(); } }

datos.js

 const customer1 = new Customer(123,"John","123 E Cookie St",'xxxx xxxx xxxx 1223'); const order1= new Order(801,'January 12, 2021 01:15:00',customer1.getNameCustomer() ); let orders=[order1];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está llamando al método getNameCustomer en customer1 que no existe. Deberías usar

 customer1.getName()

Pero puede hacerlo mejor si pasa el objeto del customer en la clase Order y lo convierte en una propiedad como:

 class Customer { constructor(id, name, address, creditCard) { this._id = id; this._name = name; this._address = address; this._creditCard = creditCard; } getName() { return this._name; } } class Order { constructor(id, date, customer) { this._id = id; this._date = date; this._customer = customer; } getNameCustomer() { return this._customer.getName(); } } const customer1 = new Customer(123, "John", "123 E Cookie St", "xxxx xxxx xxxx 1223"); const order1 = new Order(801, "January 12, 2021 01:15:00", customer1); console.log(order1); console.log(order1.getNameCustomer());
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

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!