Consider the following ES6 class:
class C {
x () { }
fnIsMethodOfC (fn) { return /* ? */ }
}
Along with various functions like
function y () {}
z = () => {}
What is an efficient way to determine if a function is a method of C, for example:
c = new C()
c.fnIsMethodOfC(c.x) === true
c.fnIsMethodOfC(C.prototype.x) === true
c.fnIsMethodOfC(y) === false
c.fnIsMethodOfC(z) === false
While one could potentially recursively search through the prototype chain, it may not be the most optimal solution.
Explore related questions here:
- JavaScript ES6: Test for arrow function, built-in function, regular function?
- Determine if a JavaScript function is a bound function
- ES6 Iterate over class methods