0

I was trying out below code.

function Foo(who){
        this.me = who;
}
Foo.prototype.identify = function(){
     return "I am " + this.me;
};
var a1 = new Foo("a1");
a1.identify();
a1.identify = function(){
     alert("Hello, " + Foo.prototype.identify.call(this));
};
a1.identify();  

Above code works well and I understand why Foo.prototype.identify.call was used. I wanted to test my understanding of this so I changed a1.identify to below

a1.identify2 = function(){
     alert("Hello, " + a1.identify());
};
a1.identify2();

I expected above to work the same way. I am now just referring to identify by direct protocol chain. However, I get undefined when I run above. Am I missing something(or misunderstanding?).

3
  • I think your not calling the function u r just printing the function. It should be this alert("Hello, " + a1.identify()); instead of alert("Hello, " + a1.identify); Commented Dec 7, 2015 at 6:56
  • 2
    It's unclear what your question actually is. There's nothing undefined in the code you have above. You can see the results here: jsfiddle.net/jfriend00/94yLngj6. You probably meant to write alert("Hello, " + a1.identify()); to actually run a1.identify() not to just print out the function itself, but when I run what you have above in the jsFiddle, nothing shows as undefined. Commented Dec 7, 2015 at 6:56
  • Thank you for your quick response. Yes, It is because I didn't have a1.identify() in alert OR not calling a1.identify2()().. thank you for making me realize this!!! Thank you both for quick answer. hmmm, also, I was running this in the chrome developer tools and was giving me back undefined when I ran the code as I presented in original post. Commented Dec 7, 2015 at 7:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed explanation. Accepted!
1
a1.identify2 = function(){
 alert("Hello, " + a1.identify());
};
a1.identify2();

a1.identify() should have been called with ().

a1.identify() calls alert() and does not return any value. In such case, an undefined is returned. So, when a1.identify2() calls a1.identify(), a1.identify() is executed and undefined is returned. Hence, you see "Hello, undefined".

If you want to see output in identify2() then in identify1() instead of alerting, return the value.

1 Comment

Thank you for your help!

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.