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

101
Visualizações
Adding accessor methods to objects from server

Still new to the JavaScript world, so bear with me...

I'm receiving an object from the server that, for the sake of argument, looks as follows:

{
    "names": [
        { "id": 99, "firstName": "John" },
        { "id": 13, "firstName": "Andreas" }
    ],
    "people": [
        { "id": 17, "nameId": 99, "age": 55 },
        { "id": 15, "nameId": 13, "age": 16 }
    ]
}

I currently have a bunch of utility methods that look like

function getPerson(serverObject, personId) {
    return serverObject.people.find(({id}) => id === personId)
}

function getName(serverObject, nameId) {
    return serverObject.names.find(({id}) => id === nameId)
}

function getFirstNameOfPerson(serverObject, personId) {
    var person = getPerson(serverObject, personId)
    return getName(serverObject, person.nameId).firstName
}

Instead of

getFirstNameOfPerson(serverObject, getPerson(serverObject, 17))

I would like to work with the serverObject as follows:

serverObject.getPerson(17).getFirstName()

So I'm thinking of either attaching a bunch of methods onto the server object when I receive it from the server, or create a separate ServerObjectAccessor class which wrapps the server object and has a bunch of access methods.

Is this a common practice? Is there a better way of doing it? Should I be using prototypes for this type of thing? I'm using Redux. Would it be appropriate to do the wrapping in the reducer and store only the ServerObjectAccessor in the state?

(I'm actually using TypeScript if it matters...)

about 4 years ago · Juan Pablo Isaza
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