检查函数是否是类的方法?

问题描述 投票:1回答:4

有没有办法确定函数是否是某个类的方法?

我有一个class A与方法doesMethodBelongHere,它采取函数作为参数method。我想确定methodA的实际方法。

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
javascript ecmascript-6
4个回答
3
投票

您可以使用Object.getPrototypeOf()来获取原型。然后使用for...ofObject.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

1
投票

您可以使用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

Edit

doesMethodBelongHere( method ) {
    return  "function" === typeof ( this[method.name] )
}

0
投票

    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班级的约束。


0
投票

我建议使用以下实现:

  1. 在构造函数原型上使用Object.getOwnPropertyNames(同样访问A.prototype,但是采用更通用的方法)以迭代类方法。
  2. 使用method.name获取方法名称
  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; } }
© www.soinside.com 2019 - 2024. All rights reserved.