1

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
  • I think Visual Studio Watcher is broken :) Because it worked on Firebug. Commented Oct 14, 2010 at 13:18

2 Answers 2

2

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') ...
Sign up to request clarification or add additional context in comments.

2 Comments

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...
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.
2

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  

1 Comment

I saw hasOwnProperty in some examples that shows Array.hasOwnProperty('push') . But in my example it doesn't work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.