有没有办法确定一个函数是否是某个类的方法?
我有一个方法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
您可以使用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] )
}
我建议使用以下实现:
- 在构造函数原型上使用 Object.getOwnPropertyNames(访问
A.prototype
相同,但采用更通用的方法(以迭代类方法。 - 使用 method.name 获取方法名称
-
使用 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; } }