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

267
Visualizações
How to show all properties and methods of an object in javascript?

vscode showing different methods, properties and events. vscode showing different methods, properties and events

Hey people,

I can see that in vscode it is possible to see all the different properties, methods and events of a given object. How can I access all of these and store them in an array, without having to hardcode it?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Object.keys(obj) will give you an array of the enumerable properties on the object.

Object.getOwnPropertyDescriptors(obj) will give you an array of "own" property descriptors for the object and will include non-enumerable properties.

Object.getOwnPropertyNames(obj) will gives you an array of "own" property names for the object and will include non-enumerable properties.

If you want to also list what methods might be on the prototype, then you'd have to use one of these above methods on the prototype which you can get with Object.getPrototypeOf(obj).

And, if this object is a subclass and you want to include methods on any base classes, you'd have to follow the prototype chain and enumerate each prototype in that chain. You follow the chain by calling Object.getPrototypeOf(obj) and then call that again on that prototype and so on until you get null. This is how you "walk" the prototype chain.

You can see the MDN doc for all of these for more info.

Here's an example:

class A {
    constructor() {
        this.propA = "A";
        Object.defineProperty(this, "propA2", { enumerable: false });
    }
    showMe() {
        console.log("I'm from class A");
    }
}

class B extends A {
    constructor() {
        super();
        this.propB = "B";
        Object.defineProperty(this, "propB2", { enumerable: false });
    }
    showMeToo() {
        console.log("I'm from class B");
    }
}

let x = new B();

// [ 'propA', 'propB' ]
console.log(Object.keys(x));

// [ 'propA', 'propA2', 'propB', 'propB2' ]
console.log(Object.getOwnPropertyNames(x));

// [ 'constructor', 'showMeToo' ]
let protoB = Object.getPrototypeOf(x);
console.log(Object.getOwnPropertyNames(protoB));

// [ 'constructor', 'showMe' ]
let protoA = Object.getPrototypeOf(protoB);
console.log(Object.getOwnPropertyNames(protoA));

// shows properties on generic Object prototype
let p1 = Object.getPrototypeOf(protoA);
console.log(Object.getOwnPropertyNames(p1));

// shows null because it's the end of the prototype chain
let p2 = Object.getPrototypeOf(p1);
console.log(p2);

about 4 years ago · Santiago Trujillo Relatório

0

You can use Object.keys(yourObj)
This returns all properties of a given object.

about 4 years ago · Santiago Trujillo 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