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

427
Visualizações
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 à 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