Solución

@alexiis-dev·28/5/2026TypeScript
solution.tsTypeScript
export function getThisType(
  context: 'method' | 'arrow' | 'unbound'
): string {

  switch (context) {

    case 'method': {

      const obj = {
        name: 'Pedro',
        test() {
          return this;
        }
      };

      return obj.test() === obj
        ? 'this es el objeto'
        : '';
    }

    case 'arrow': {

      class Person {

        name = 'Pedro';

        test = () => {
          return this;
        };
      }

      const p = new Person();

      const extracted = p.test;

      return extracted() === p
        ? 'this es la instancia'
        : '';
    }

    case 'unbound': {

      class Person {

        name = 'Pedro';

        test() {
          return this;
        }
      }

      const p = new Person();

      const extracted = p.test;

      try {

        return extracted() === undefined
          ? 'this es undefined'
          : '';

      } catch {

        return 'this es undefined';
      }
    }
  }
}
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.