0

I was reading this article, and I took the example and made some very small modification

function Parent( p1, p2 ) {
 console.log('run here');
 var prop1 = p1; // private property
 this.prop2 = p2; // public property
 // private method
 function meth1() { return prop1; }
 // public methods
 this.meth2 = function(){ return this.prop2; };
 this.meth6 = function(){ return meth1(); };
}
Parent.prototype.hello=function(){console.log('hi '+this.prop2);};
function Child( p1, p2, p3, p4 ) {
 Parent.apply( this, arguments ); // initialize Parent's members
 this.prop3 = p3;
 this.meth3 = function(){ return this.prop3; }; // override
 var prop4 = p4;
 this.meth4 = function(){ return prop4; };
 }
Child.prototype = new Parent(); // set up inheritance relation
// console.log(Child.prototype)
var cc = new Child( "one", "two", "three", "four" );
console.log(cc)
var result1 = cc.meth6(); // parent property via child
var result2 = cc.meth2(); // parent method via child
var result3 = cc.meth3(); // child method overrides parent method
var result4 = cc.hello(); 
console.log(result1,result2,result3,result4);

The problem is even cc.hello method seems like exist, but when I call it, the console return undefined, can someone please explain why? thanks

asked Aug 13, 2015 at 22:15
1
  • 3
    The hello method doesn't return anything, thus undefined. Not sure what you expect... Commented Aug 13, 2015 at 22:17

1 Answer 1

1

The method hello doesn't return a value, thus it is undefined

You can change the method to something like this to return the value:

Parent.prototype.hello = function(){
 return 'hi '+ this.prop2;
};
answered Aug 13, 2015 at 22:18
Sign up to request clarification or add additional context in comments.

1 Comment

Hahaha, thanks, I guess my brain was short circuited :P

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.