Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
concatenative inheritance two methods with same signature

The code below prints only the event contents, how do I make that print WheatherData.value number? I am coming from java and I am used to do it this way.

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 Respostas
Responde à pergunta

0

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

The line above causing the problem. When you assign _event object to { getValue, toString } object you are simply overriding toString method of WeatherData function. Instead, just return { getValue, toString } from your WeatherData function, like below;

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 Relatório

0

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

this works for me, is it the right way to do that? I overwrite the event's toString method this way

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda