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

248
Vistas
Use this as parameter name in javascript function definition

I saw a function definition like this in my learning vscode source code.

export function once<T extends Function>(this: unknown, fn: T): T {
    const _this = this;
    let didCall = false;
    let result: unknown;

    return function () {
        if (didCall) {
            return result;
        }

        didCall = true;
        result = fn.apply(_this, arguments);

        return result;
    } as unknown as T;
}

But this argument is not passed when calling

... ...
const self = trackDisposable({
    dispose: once(() => {
        markAsDisposed(self);
        fn();
    })
});
... ...

I want to determine if javascript will automatically pass this argument in this case

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Paraphrasing the typescript documentation on this

Declaring this in a Function

TypeScript will infer what this should be in a function via code flow analysis

But, there are a lot of cases where you need more control over what object this represents. Since the JavaScript specification states that you cannot have a parameter called this, TypeScript uses that syntax space to let you declare the type for this in the function body.

This is common with "callback-style" APIs, where another object typically controls when your function is called. Note that you need to use function, and not an arrow function, to get this behaviour

In other words

export function once<T extends Function>(this: unknown, fn: T): T {

simply defines that this will be unknown, but the function takes a single argument, fn : T

about 4 years ago · Juan Pablo Isaza 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