nodejs 如何实现 php 的 __callStatic 魔术方法 ?

nodejs 如何实现 php 的 __callStatic 魔术方法 ?
或者说, 如何 如何使用 nodejs 实现 static 对 普通方法的呼叫 ?

阅读 2.8k
1 个回答

可以尝试用Prixy来实现,但是需要调用代理后的对象而不是原类

(()=>{
    class Test{
        static staticMethod(){
            console.log(`this is staticMethod`);
        }
    }

    class Test2 extends Test{
        static staticMethod2(){
            console.log(`this is staticMethod2`);
        }
    }

    var P = new Proxy(Test, {
        apply: (target, that, args) => {
            console.log("apply", target, that, args);
        },
        get: (target, property, receiver) => {
            if(property in target){
                return target[property];
            }else{
                return Test2.staticMethod2;
            }
        }
    });
    P.staticMethod();
    P.staticMethod2();
})();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题