Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!