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

159
Views
herencia concatenativa dos métodos con la misma firma

El siguiente código imprime solo el contenido del evento, ¿cómo hago para que se imprima el número de WheatherData.value? Vengo de Java y estoy acostumbrado a hacerlo de esta manera.

 const Event = (time, place) => { var _time = time var _place = place return { getTime() { return _time }, getPlace() { return _place }, updateTime() { _time = new Date(Date.now()) }, toString() { return 'Time: ' + _time + '\nPlace: ' + _place } } } const WeatherData = (value, time, place) => { var _event = Event(time, place) var _value = value const getValue = () => { return _value } const toString = () => { return '\nValue: ' + _value + _event.toString() } return Object.assign({ getValue, toString }, _event) } const wd = WeatherData(10.1, new Date(Date.now()), 'Chisinau, Moldova') console.log(wd.toString()) //Time: Sat Sep 18 2021 08:49:10 GMT+0200 (Central European Summer Time) //Place: Chisinau, Moldova // no value printed

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

return Object.assign({ getValue, toString }, _event)

La línea de arriba que causa el problema. Cuando asigna el objeto _event al objeto { getValue, toString } simplemente está anulando el método toString de la función WeatherData . En su lugar, simplemente devuelva { getValue, toString } desde su función WeatherData , como se muestra a continuación;

 const WeatherData = (value, time, place) => { var _event = Event(time, place); var _value = value const getValue = () => { return _value } const toString = () => { return '\nValue: ' + _value + '\n' + _event.toString() } return { getValue, toString } }
about 4 years ago · Juan Pablo Isaza Report

0

return Object.assign({}, _event, { getValue, setValue, toString })

esto funciona para mí, ¿es la forma correcta de hacerlo? Sobrescribo el método toString del event de esta manera

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!