How can I check if a method is defined in a JavaScript class

I need to check if a class definition either through inheritance or not provides a specific method. Do I need to move the prototype chain to accomplish this?

function TestClass(config){ //NOTE: cannot instantiate class because if config not valid Error is thrown } TestClass.prototype.sampleMethod = function(){}; function isDefined(klass){ console.log(typeof klass.sampleMethod); //'undefined' console.log('sampleMethod' in klass); //false console.log(klass['sampleMethod']); //undefined console.log(typeof klass.prototype.sampleMethod); //'function' ...inheritance? } isDefined(TestClass); 
+4
source share
4 answers

Yes, you need to check the prototype chain.

 function TestClass(config){} TestClass.prototype.sampleMethod = function(){}; function TestClass2() {} TestClass2.prototype = TestClass; function isDefined(obj, method) { if (obj.prototype !== undefined) { var methodInPrototype = (method in obj.prototype); console.log("Is " + method + " in prototype of " + obj + ": " + methodInPrototype); if (methodInPrototype) { return true; } else { isDefined(obj.prototype, method); } } return false; } isDefined(TestClass, "sampleMethod"); isDefined(TestClass2, "sampleMethod"); isDefined(TestClass2, "sampleMethod2"); 

Print

 // Is sampleMethod in prototype of function TestClass(config) {}: true // Is sampleMethod in prototype of function TestClass2() {}: false // Is sampleMethod in prototype of function TestClass(config) {}: true // Is sampleMethod2 in prototype of function TestClass2() {}: false // Is sampleMethod2 in prototype of function TestClass(config) {}: false 
0
source

I think the problem may be that you cannot determine whether the class implements something directly without looking at the class instance, unless you specifically assign it to the prototype of the new class for verification. Remember that when the prototype of your class is assigned the sampleMethod property, it is an instance object that represents the prototype, not the class. In fact, classes do not actually exist in JavaScript.

 function TestClass(config){} TestClass.prototype.sampleMethod = function(){}; function isDefined(klass){ var holder, _defined = false; try{ holder = new klass({}); _defined = typeof holder.sampleMethod !== 'undefined'; // does the prototype lookup for you }catch(e){ console.log('Error loading class for reasons other than invalid method.', e) }finally{ return _defined; } } 
+1
source

Is that what you gonna do?

 function TestClass(config) {} TestClass.prototype.sampleMethod = function() {}; function isDefined(klass, method) { return (klass && method ? function() { return !!klass.prototype[method]; }() : false); } 

An example of what this does: http://fiddle.jshell.net/Shaz/2kL9A/

0
source

I don’t understand why you are testing the constructor, I would just check the object directly to find out if it has a specific method.

Anyway, Gabriel is pretty close, but I would have done it differently:

 function MyConstructor(){} MyConstructor.prototype.sampleMethod = function(){}; // Return true if instances of constructor have method // Return false if they don't // Return undefined if new constructor() fails function isDefined(constructor, method){ var temp, defined; try { temp = new constructor(); defined = !!(typeof temp[method] == 'function'); } catch(e) { // calling - new constructor - failed } return defined; } var foo; alert( isDefined(MyConstructor, 'sampleMethod') // Method on MyConstructor.prototype + '\n' + isDefined(MyConstructor, 'undefinedMethod') // Not defined + '\n' + isDefined(MyConstructor, 'toString') // Method on Object.prototype + '\n' + isDefined(foo, 'foo') // foo is not a constructor ); 

Of course, the above is only suitable for classical prototype inheritance using [[prototype]], something else is required when using a module template or similar (ie "inheritance" using closures).

0
source

Source: https://habr.com/ru/post/1347440/


All Articles