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



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

我有一个方法class AdoesMethodBelongHere,它将函数作为参数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

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

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

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

我建议使用以下实现:

  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;
    }
    }
    

相关内容

最新更新