tags:

views:

91

answers:

2

Consider the following sample:

var Container = function(param) {
    this.member = param;
    var privateVar = param;
    if (!Container.prototype.stamp) {  // <-- executed on the first call only
        Container.prototype.stamp = function(string) {
            return privateVar + this.member + string;
        }
    }
}

var cnt = new Container();

Is there any way to determine whether the object cnt has a method named stamp without knowing that it is instantiated from Container ?

Another Example

+1  A: 

You can use hasOwnProperty

o = new Object();  
o.prop = 'exists';  
o.hasOwnProperty('prop');             // returns true  
o.hasOwnProperty('toString');         // returns false  
o.hasOwnProperty('hasOwnProperty');   // returns false  
AutomatedTester
I saw hasOwnProperty in some examples that shows `Array.hasOwnProperty('push')` . But in my example it doesn't work!
uzay95
+1  A: 

You can test for the existence of stamp with:

if (cnt.stamp) ...

or you can check whether it is a function with

if (typeof cnt.stamp === 'function') ...
Tomas
Please check my last image. Your offer doesn't works!!! :(
uzay95
it does work if you test it seperately in a javascript console: `var a = { b: function() {} }; typeof a.b` . Maybe f_SetEk is not a function? It sais it's an undefined identifier in your image...
Tomas
No it is function but in visual studio Watch window it doesn't says it is function. But you are right it is working well in browser.
uzay95