鍍金池/ 問答/PHP  HTML/ nodejs 如何實現(xiàn) php 的 __callStatic 魔術(shù)方法 ?

nodejs 如何實現(xiàn) php 的 __callStatic 魔術(shù)方法 ?

nodejs 如何實現(xiàn) php 的 __callStatic 魔術(shù)方法 ?
或者說, 如何 如何使用 nodejs 實現(xiàn) static 對 普通方法的呼叫 ?

回答
編輯回答
笑浮塵

可以嘗試用Prixy來實現(xiàn),但是需要調(diào)用代理后的對象而不是原類

(()=>{
    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();
})();
2018年9月8日 10:23