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

148
Vistas
ES6 Iterar sobre métodos de clase

Dada esta clase; ¿Cómo iteraría sobre los métodos que incluye?

 class Animal { constructor(type){ this.animalType = type; } getAnimalType(){ console.log('this.animalType: ', this.animalType ); } } let cat = window.cat = new Animal('cat')

Lo que he intentado es lo siguiente sin éxito:

 for (var each in Object.getPrototypeOf(cat) ){ console.log(each); }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Puede usar Object.getOwnPropertyNames en el prototipo:

 Object.getOwnPropertyNames( Animal.prototype ) // [ 'constructor', 'getAnimalType' ]
over 4 years ago · Santiago Trujillo Denunciar

0

Lo sé, lo sé, pero bueno...

 const isGetter = ( x, name ) => ( Object.getOwnPropertyDescriptor( x, name ) || {} ).get const isFunction = ( x, name ) => typeof x[ name ] === "function"; const deepFunctions = x => x && x !== Object.prototype && Object.getOwnPropertyNames( x ) .filter( name => isGetter( x, name ) || isFunction( x, name ) ) .concat( deepFunctions( Object.getPrototypeOf( x ) ) || [] ); const distinctDeepFunctions = x => Array.from( new Set( deepFunctions( x ) ) ); const userFunctions = x => distinctDeepFunctions( x ).filter( name => name !== "constructor" && !~name.indexOf( "__" ) ); // example usage class YourObject { hello() { return "uk"; } goodbye() { return "eu"; } } class MyObject extends YourObject { hello() { return "ie"; } get when() { return "soon"; } } const obj = new MyObject(); console.log( userFunctions( obj ) ); // [ "hello", "when", "goodbye" ]

over 4 years ago · Santiago Trujillo Denunciar

0

Esto es un poco más elaborado pero obtiene métodos de toda la cadena de prototipos.

 function getAllMethodNames (obj, depth = Infinity) { const methods = new Set() while (depth-- && obj) { for (const key of Reflect.ownKeys(obj)) { methods.add(key) } obj = Reflect.getPrototypeOf(obj) } return [...methods] }
over 4 years ago · Santiago Trujillo Denunciar
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