有没有办法确定函数是否是某个类的方法?
我有一个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
您可以使用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] )
}
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
班级的约束。
我建议使用以下实现:
A.prototype
,但是采用更通用的方法)以迭代类方法。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;
}
}