Code Monkey home page Code Monkey logo

Comments (6)

whtiehack avatar whtiehack commented on August 25, 2024

image
是不是因为你没有new出来。你new出来试试。

from pinus.

superyyl avatar superyyl commented on August 25, 2024

image
是不是因为你没有new出来。你new出来试试。

Sorry, 我实际代码有写new, 贴上来时候漏了,已编辑上

export default function只有子类写了,基类只有在constructor里用super调用

from pinus.

superyyl avatar superyyl commented on August 25, 2024

image
是不是因为你没有new出来。你new出来试试。

我先看一下pinus-rpc/utils/utils.ts里的listEs6ClassMethods方法,应该是这个没把父类方法过滤出来

from pinus.

superyyl avatar superyyl commented on August 25, 2024

class BaseHandler {
    foo() {

    }
}

class MyHandler extends BaseHandler {
    bar() {

    }
}

function listEs6ClassMethods(objInstance: { [key: string]: any }) {
    if (objInstance.prototype && objInstance.prototype.constructor === objInstance) {
        let names: string[] = [];
        let methodNames = Object.getOwnPropertyNames(objInstance.prototype);
        for (let name of methodNames) {
            let method = objInstance.prototype[name];
            // Supposedly you'd like to skip constructor
            if (!(method instanceof Function) || name === 'constructor') continue;
            names.push(name);
        }
        return names;
    } else {
        let names: string[] = [];
        let methodNames = Object.getOwnPropertyNames(Object.getPrototypeOf(objInstance)).concat(Object.getOwnPropertyNames(objInstance));
        for (let name of methodNames) {
            let method = objInstance[name];
            // Supposedly you'd like to skip constructor
            if (!(method instanceof Function) || name === 'constructor') continue;
            names.push(name);
        }
        return names;
    }
}

let ret = listEs6ClassMethods(new MyHandler());

console.log(ret);

print:[ 'bar' ]

from pinus.

whtiehack avatar whtiehack commented on August 25, 2024

文件 pinus/packages/pinus-rpc/lib/util/utils.ts

function listEs6ClassMethods(objInstance: { [key: string]: any }) {
    if (objInstance.prototype && objInstance.prototype.constructor === objInstance) {
        let names: string[] = [];
        let methodNames = Object.getOwnPropertyNames(objInstance.prototype);
        for (let name of methodNames) {
            let method = objInstance.prototype[name];
            // Supposedly you'd like to skip constructor
            if (!(method instanceof Function) || name === 'constructor') continue;
            names.push(name);
        }
        return names;
    } else {
        let names: string[] = [];
        let methodNames = Object.getOwnPropertyNames(Object.getPrototypeOf(objInstance)).concat(Object.getOwnPropertyNames(objInstance));
        for (let name of methodNames) {
            let method = objInstance[name];
            // Supposedly you'd like to skip constructor
            if (!(method instanceof Function) || name === 'constructor') continue;
            names.push(name);
        }
        return names;
    }
}

具体的代码是这个 现在有事不能研究了。

我测试了一下,的确不能获取到父类的方法。


class BaseHandler {
    protected app;
    constructor(app) {
        this.app = app;
    }
    public async who(msg, session) {
        return { myname: "this.constructor.name" };
    }
}


class MyHandler extends BaseHandler {
    constructor(app) {
        super(app);
    }
    public async myMethod(msg, session) {
        return { msg: 'this is my method' };
    }
}



console.log(listEs6ClassMethods(MyHandler))

let ins = new MyHandler(null)
console.log(listEs6ClassMethods(ins))
console.log(ins.who(null,null))
console.log(Object.getPrototypeOf(ins).constructor.prototype)
// 这一句才可以获取到父类的方法
console.log(Object.getPrototypeOf(ins.constructor.prototype))

你可以自己copy一下 dictionary组件 自己处理一下这个情况

用这名代码替换掉框架自带的 dictionary.

pinus.components.dictionary = DictionaryComponent;

from pinus.

superyyl avatar superyyl commented on August 25, 2024

@whtiehack 感谢

from pinus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.