Reputation: 89
What's the difference between query(userOptions)
and query(...args)
? I get that they are probably overloads for the same function call, but I'm not sure how they would be actually called or under what circumstances in a calling program to select which overload is used, especially with the static
decoration.
class Gamedig {
constructor() {
this.queryRunner = new QueryRunner();
}
async query(userOptions) {
return await this.queryRunner.run(userOptions);
}
static getInstance() {
if (!singleton) singleton = new Gamedig();
return singleton;
}
static async query(...args) {
return await Gamedig.getInstance().query(...args);
}
}
Upvotes: 1
Views: 687
Reputation: 816364
static
means that the function is assigned to the constructor, i.e. Gamedig
. The function is called as
Gamedig.query(...)
Without static
the function is accessible on an instance if Gamedig
, i.e.
var instance = new Gamedig();
instance.query(...);
JavaScript doesn't support function overloading.
userOptions
vs ...args
has nothing to do with that. The rest parameter in a function definition means that the function accepts a variable number of arguments and they are all collected in an array which is assigned to that parameter (args
in your example).
Example:
function foo(...bar) {
console.log(bar);
}
foo(1);
foo(1,2);
foo(1,2,3);
Upvotes: 2