How to use Proxy to simulate PHP's __callStatic attribute in Node.js
P粉653045807
2023-09-04 09:05:21
<p>I'm trying to create the same behavior in Node.js as the PHP <code>__callStatic</code> magic method. </p>
<p>I'm trying to do it using <code>Proxy</code> but I'm not sure if it's the best option. </p>
<p>
<pre class="snippet-code-js lang-js prettyprint-override"><code>class Test {
constructor() {
this.num = 0
}
set(num) {
this.num = this.num num
return this
}
get() {
return this.num
}
}
const TestFacade = new Proxy({}, {
get: (_, key) => {
const test = new Test()
return test[key]
}
})
//The execution method chain ends with get
console.log(TestFacade.set(10).set(20).get())
//Expected result: 30
//Return result: 0
// Start a new execution method chain and instantiate the Test class again in the first set
console.log(TestFacade.set(20).set(20).get())
//Expected result: 40
// Return result: 0</code></pre>
</p>
<p>The problem is that every time I try to access the properties of <code>TestFacade</code>, the <code>get</code> trap is triggered. The behavior I need is that when the <code>set</code> method is called it will return <code>this</code> of the <code>Test</code> class and I can even save that instance for later use! </p>
<pre class="brush:php;toolbar:false;">const testInstance = TestFacade.set(10) // The set method returns `this` of `Test` instead of Proxy</pre>
<p>If anything is unclear, please let me know. </p>
I don't know if this is the best option. But I solved the problem by returning a new proxy in the
get
trap that binds thetest
class instance into the method using theapply
trap :