Проверить, является ли функция методом класса? - PullRequest
0 голосов
/ 19 октября 2018

Есть ли способ определить, является ли функция методом определенного класса?

У меня есть class A с методом doesMethodBelongHere, который принимает функцию в качестве аргумента method.Я хочу определить, что method является действительным методом A.

class A {
  methodA() {
    console.log('method of A');
  }
  
  doesMethodBelongHere(method) {
    // it should return true if `method` argument is a method of A
    return Object.values(this).includes(method);
  }
}

const a = new A(); 
console.log(a.doesMethodBelongHere(a.methodA)); // should return true

Ответы [ 4 ]

0 голосов
/ 19 октября 2018

Я бы предложил использовать следующую реализацию:

  1. Использовать Object.getOwnPropertyNames в прототипе конструктора (тот же самый доступ к A.prototype, но в более общем подходе) для итерации методов класса.
  2. Получите имя метода, используя имя метода
  3. Используя Array.some , найдите (1) включает название данного метода.

    class A {
    
        constructor() {
            this.a = 2;
            this.bb = 3;
        }
        methodA() {
            console.log('method of A');
        }
    
        doesMethodBelongHere(method) {
            // it should return true if `method` argument is a method of A
            return this.constructor.prototype[method.name] === method;
        }
    }
    
0 голосов
/ 19 октября 2018

Вы можете использовать Object.getPrototypeOf(), чтобы получить прототип.Затем выполните итерации свойств прототипа, используя for...of и Object.getOwnPropertyNames().Если метод равен одному из методов прототипа, верните true:

class A {
  methodA() {
    console.log('method of A');
  }

  doesMethodBelongHere(method) {
    // get the prototype
    const proto = Object.getPrototypeOf(this);
    
    // iterate the prototype properties, and if one them is equal to the method's reference, return true
    for(const m of Object.getOwnPropertyNames(proto)) {
      const prop = proto[m];
      if(typeof(prop) === 'function' && prop === method) return true;
    }
    
    return false;
  }
}

const a = new A();
Object.assign(a, { anotherMethod() {} }); 
a.anotherMethod2 = () => {};

console.log(a.doesMethodBelongHere(a.methodA)); // should return true

console.log(a.doesMethodBelongHere(a.anotherMethod)); // should return false

console.log(a.doesMethodBelongHere(a.anotherMethod2)); // should return false

Расширенные классы:

Это решение также будет обрабатывать методы, полученные из расширенного класса:

class A {
  methodA() {
    console.log('method of A');
  }

  doesMethodBelongHere(method) {
    let proto = this;
    
    // iterate the prototypes chain
    while (proto = Object.getPrototypeOf(proto), proto && proto !== Object) {
      // iterate the prototype properties, and if one them is equal to the method's reference, return true
      for (const m of Object.getOwnPropertyNames(proto)) {
        const prop = proto[m];
        if (typeof(prop) === 'function' && prop === method) return true;
      }
    }

    return false;
  }
}

class B extends A {}

class C extends B {}

const c = new C();
Object.assign(c, {
  anotherMethod() {}
});

c.anotherMethod2 = () => {};

console.log(c.doesMethodBelongHere(c.methodA)); // should return true

console.log(c.doesMethodBelongHere(c.anotherMethod)); // should return false

console.log(c.doesMethodBelongHere(c.anotherMethod2)); // should return false
0 голосов
/ 19 октября 2018

Вы можете использовать typeof оператор

let isfn = "function" === typeof ( a.methodA );//isfn should be true
isfn = "function" === typeof ( a["methodA"] );//isfn should be true
isfn = "function" === typeof ( a["methodAX"] );//isfn should be false

Редактировать

doesMethodBelongHere( method ) {
    return  "function" === typeof ( this[method.name] )
}
0 голосов
/ 19 октября 2018

    class A {
      constructor() {
        this.methodA = this.methodA.bind(this);
        this.doesMethodBelongHere = this.doesMethodBelongHere.bind(this);
      }
    	methodA() {
        console.log('method of A');
      }
      
      doesMethodBelongHere(method) {
        // it should return true if `method` argument is a method of A
        return Object.values(this).includes(method);
      }
    }

    const a = new A(); 
    console.log(a.doesMethodBelongHere(a.methodA)); // should return true

Это не было привязано к вашему классу в вашем doesMethodBelongHere.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...