There are lots of questions on here about this
, but “multiple this args” is getting me answers about how to declare multiple properties under this
, which isn’t what I’m after. If this is already asked and answered elsewhere, I’ll take the pointer!
I have a Prisma extension I’m writing, where the methods I’m creating rely on access to a certain this
. But now I’m re-writing the top level of my project as a class, and I want those methods to be able to access the existing this
, but also the classThis
“this” of the class in which they’re declared. Also, I need to be able to pass in this.someVar
to methods which are arrow functions and maintaining their own this
.
In more detail,
// file: method1.mjs
import { Prisma } from '@prisma/client';
/**
* @template T
* @this {T}
*/
export async function method1 (args) {
const ctx = Prisma.getExtensionContext(this) // this "this" needs to remain
// I would like to do this, where `classThis` is the "this" of the class that
// imports this function and uses it as a method
const test = classThis.sayHello('Hello!');
}
// file myClass.mjs
import { method1 } from 'method1.mjs';
export class MyClass {
private readonly configArg;
constructor ( configArg ) {
this.configArg = configArg;
}
sayHello = (greeting) => console.log(greeting);
method1 = async (args) => {
// putting in an arrow function and using `call` so Prisma
// gets the correct `this` in the method defined above
return await method1.call(this, {
...args,
// also, I think this `this` is no longer the class?
this.configArg
});
}
}
I believe the .call(this
is working, but the missing pieces are how do I get method1
to have that this
, and a this
that lets it call the other methods (e.g., this.sayHello
); and in the call to the imported module (which I think has to be an arrow function to get the correct this
to prisma), how do I also pass the this.someProp
properties from the enclosing class? (configArg
in the example).
4
I’m not familiar with Prisma specifically, and you didn’t provide details on how the method is being called, but this code should demonstrate the techniques you need to use to solve your problem. I’ll even demonstrate how to bring in another special
argument in case you need it.
function method1(...args) {
console.log('`this` is:', this);
console.log('`args` are:', args);
}
let special = { id: 'special' };
class MyClass {
constructor () {
const me = this;
this.example = function (...args) {
return method1.call(this, me, special, ...args);
}
}
}
const myInstance = new MyClass();
myInstance.id = 'myInstance';
console.log('calling with myInstance');
myInstance.example(1, 2, 3);
console.log('calling with otherInstance');
const otherInstance = { id: 'otherInstance' };
otherInstance.example = myInstance.example;
otherInstance.example(4, 5, 6);
Feel welcome to replace method1
with function method1(me, special, ...args)
to help organize the extra arguments.
In this case you can call the method with another instance from the class on which it was defined, and you can have access to the original class and any other special instance.