I can't quite tell what your question is. When I run your code in a jsFiddle, there's nothing undefined in it. You are doing an alert() on a1.identify rather than a1.identify() so you weren't actually calling the function and I'm guessing that was one mistake of yours. Instead a1.identify will just attempt to do a .toString() conversion on the method which will attempt to dump the source of the function (that's what my jsFiddle or your code shows).
So, probably, you meant to do this:
a1.identify2 = function(){
alert("Hello, " + a1.identify());
// parens added here ^^
};
a1.identify2();
Perhaps what you may need explained is that these two can be somewhat different things:
Foo.prototype.identify
and
a1.identify
If nothing has been assigned directly to a1.identify, then executing a1.identify() will not find the .identify property directly on the a1 object so it will then look on the Foo.prototype and it will find the property name there and execute that one on the prototype.
But, as soon as you do this:
a1.identify = function() {...}
Then, you've "overriden" this property. Now, when you do:
a1.identify()
The JS interpreter finds your overriden property directly on the a1 object and that is executed instead of the property that resides on the prototype. When you do obj.property, the JS interpreter looks first for that property directly on the object and only if it's not found assigned directly to the object does it then search the object's prototype for a property with that name.
As you seem to know, you can always get to the function that's on the prototype, even if the property has been overriden directly on the object by going directly through the prototype with:
Foo.prototype.identify.call(a1)
So, you can override a prototype property for a specific instance and, even if you've overriden it, you can still get to the original property on the prototype if you need to or want to.
alert("Hello, " + a1.identify());instead ofalert("Hello, " + a1.identify);undefinedin the code you have above. You can see the results here: jsfiddle.net/jfriend00/94yLngj6. You probably meant to writealert("Hello, " + a1.identify());to actually runa1.identify()not to just print out the function itself, but when I run what you have above in the jsFiddle, nothing shows asundefined.