Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda