1
function object1(){


}

object1.prototype.myMethod = function(){};

function object2(){

    this.myMethod = function(){};

}

I need to write an if statement that can check if any given object has the myMethod function, without creating an instance of said object. Is this possible?

This: testMe.prototype.hasOwnProperty('myMethod') would work only for object1, but for object2 will return false.


What is this for? I'm trying to emulate interfaces. I need to check if a function respects my interface before working on it and I want to leave the user total freedom on how this function is declared. Creating an instance of that function to check for it's properties sounds like opening a bottle with "water" written on it to check if there is water inside.

Saturnix
  • 10,130
  • 17
  • 64
  • 120
  • 2
    If methods are added during construction, then aside from examining source code, the only way would be to construct an object. So no, it isn't possible. – cookie monster Apr 22 '14 at 16:22
  • so, the answer is "no, it is not possible"? – Saturnix Apr 22 '14 at 16:24
  • Maybe this is what you want http://stackoverflow.com/questions/3007460/javascript-check-if-anonymous-object-has-a-method – sam Apr 22 '14 at 16:24
  • @sam: If I've understood him, he wants to analyze it based on the constructor function, not the resulting object instance. – cookie monster Apr 22 '14 at 16:26
  • @Sam: not exactly. If a user provide an object like object2, I wanna be able to check if that object respects my interface before crashing the entire program. The object listed on that question is different from object2. – Saturnix Apr 22 '14 at 16:26
  • @Saturnix: It would add clarity if you'd refer to `object2` as a `function` if that's what you're referring to. If you're not talking about the constructor, and are indeed talking about the object instance, then it's a very simple test. – cookie monster Apr 22 '14 at 16:27
  • This is confusing. At first it sounds like you need to determine this from the constructor, but then you say *"...I need to check if an object respects my interface before working on it"*, so you already have the object? If so, why would you need to avoid creating an instance when you already have one. – cookie monster Apr 22 '14 at 16:31
  • @cookiemonster, you're right. Edited! – Saturnix Apr 22 '14 at 16:33
  • @cookiemonster, you made my think that, yes, after I've verified the compliance with the interface I do indeed instantiate the object. So I could check for the method right after the instantiation but before calling said method. I'd have loved to separate verification and usage but, hey... This seems like a good solution too. – Saturnix Apr 22 '14 at 16:35
  • So your code anticipates receiving a constructor, but you don't want to use the constructor if its resulting object doesn't meet the interface. This would seem to imply that at some point there will be an object constructed. If so, you may just need to do the check at that point. – cookie monster Apr 22 '14 at 16:36
  • ...just saw your comment. Yes, I think that would be the way to go. – cookie monster Apr 22 '14 at 16:37
  • @cookiemonster, yeah but that kind of sucks. I use the method inside of a loop so I'm checking hundreds of time a thing which I should in theory check just one time. I guess I'll stick with the water bottle solution: create one instance and verify on that. I feel like an idiot in doing so though... – Saturnix Apr 22 '14 at 16:39
  • I don't know how your application is structured, but I'd be inclined to test in the first loop iteration *(if we're talking about loops)*, and then set a flag based on the test result. Probably roll out the testing into a separate function to keep it clean. All depends on what you have going on though. – cookie monster Apr 22 '14 at 16:41
  • ...also, you can still test early for the `.prototype` method, though I suppose technically it could be overwritten in the constructor. – cookie monster Apr 22 '14 at 16:43
  • ...or don't check, and just let the program throw an error if the interface isn't met. IMO, it's better to put the burden on the end user to correctly follow your API than to try to recover from their bad code. – cookie monster Apr 22 '14 at 16:44
  • Yeah, the function execution is interrupted if the interface is not met. Though, I always avoid crashing the entire program. – Saturnix Apr 22 '14 at 16:46

2 Answers2

1

There is no way you can check if an object will have some instance methods without instantiating it, because you can add a new method anywhere:

var myObj= {};
...

if (myObj.newMethod) { //false
...
}

myObj.newMethod=function(){...};

if (myObj.newMethod) { //true
...
}
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0

The second one isn't working without instantiating due to assigning this method on instantiating object, only. The code in body of function object2 is its "constructor" and it's run on instantiation only, obviously.

Thomas Urban
  • 4,649
  • 26
  • 32