Javascript prototypal inheritance weirdness?

Change: Dog. _super = Animal. Prototype; Dog.

Woof = function(){ console. Log( "Woof! " ); _super.speak(); } To: // Dog.prototype.

_super = Animal. Prototype; Log( "Woof! " ); this.speak(); }.

Ah, I think I see my mistake now. Ug - I was trying to add the function to the Dog function, which is a function object, not a Dog object. Man alive grokking prototype inheritance is tricky!Thanks.

– javamonkey79 Oct 12 '11 at 4:48 @javamonkey79 I made a correction to my answer; it now logs I'm a Dog, I can't really talk instead of I'm a Animal, I can't really talk – NullUserException? Oct 12 '11 at 4:52 oh, cool - I think either solution is fine...I had the "ah ha" moment of what I was doing wrong. Thank God for SO :) – javamonkey79 Oct 12 '11 at 4:57 @javamonkey79 Oh, you can remove the reference to _super unless you need it for some other reason.

You don't really need here. – NullUserException? Oct 12 '11 at 5:08.

The last string of the Dog pseudo-class definition is wrong. It should be Dog.prototype. Woof = function(){ console.

Log( "Woof! " ); Dog. _super.speak.

Call(this); } You should define method woof as the property of the Dog's prototype. _super is available only as the property of the Dog constructor. You should call the methods of the parent class in context of the current instance.

1 for teaching call – Deeptechtons Oct 12 '11 at 4:52 +1 For making it work correctly and making me realize I had a mistake in my answer. – NullUserException? Oct 12 '11 at 4:53 very nice answer, thanks for explaining it :) – javamonkey79 Oct 12 '11 at 4:58.

So your woof method is actually effectively a static method (If you're coming from java. Basically, it's hanging off the Dog function, and can be accessed without an instance of Dog. Ie: Dog.woof()) To get it working with an instance of a dog, you want to make sure it's a prototype definition (again, with a Java analogy, effectively a instance method definition).

As qwertymik said, Dog.prototype. Woof = function(){ console. Log( "Woof!" ); this.speak(); } Then you'll be able to do var foo = new Dog(); foo.woof().

Right, I think I thought about this when I had my "ah ha" moment due to @NullUserExceptions answer, however, this is good that you bring it up for others +1 thanks! – javamonkey79 Oct 12 '11 at 5:00 You're Welcome :) – Gopherkhan Oct 12 '11 at 17:08.

Maybe you mean to do this: Dog.prototype. _super = Animal. Prototype; Dog.prototype.

Woof = function(){ console. Log( "Woof! " ); this.

_super.speak(); }.

I couldn't get this one to work. I am using node. Js standalone exe.

– javamonkey79 Oct 12 '11 at 4:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions