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

425
Vistas
Javascript equivalent of __getitem__ in python

Suppose I have a class such as this in python:

class Table:
    """Class which provides users"""

    def __init__(self):
        self.users = [None] * 8
        pass

    def __len__(self):
        return 8

    def __getitem__(self, key):
        # get users inside a table by index
        if not 0 <= key <= 7:
            raise IndexError("User must be an integer in the range: 0-7.")
        if self.users[key] is None:
            self.users[key] = User(self, key)
        return self.users[key]

If I were to now create an instance of table and reference table like this:

my_table = Table()
my_table[3] # indexes the third user of the table, behind the scenes python creates a new User object and puts it into the list at index 3 if there is nothing at index 3 already

The User class isn't relevant to what I'm trying to do. I'm wondering if there's a way to make the number-indexable parts of a javascript object initiated from a class into dynamically generated properties of that object.

In short, I want to do this (or something that achieves the same functionality):

myTable = new Table()
myTable[3] // or even myTable.3 if that's a thing

I'm sorry for no sample JavaScript code, I just have no idea where to start on the functionality I'm trying to achieve.

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