Prototypal inheritance question in javascript?

It's the difference between modifying and replacing the prototype function A(name){ this. Name = name; } a = new A("brad"); // Change, don't replace. A.prototype.

Talk = function(){ return "hello " + this. Name; }; a.talk() // works be = new A("john"); b.talk() // works Here is what is going on: Continued from above var old_proto = A. Prototype; // Nuke that proto A.

Prototype = { talk: function() { return "goodbye " + this. Name; } }; var c = new A("Al"); a.talk() // hello brad b.talk() // hello john c.talk() // goodbye Al old_proto. Say_goodbye = function() { return "goodbye " + this.Name; }; a.

Say_goodbye() // goodbye brad b. Say_goodbye() // goodbye john c. Say_goodbye() // TypeError c.

Say_goodbye is not a function.

It's the difference between modifying and replacing the prototype. Function A(name){ this. Name = name; } a = new A("brad"); // Change, don't replace.A.prototype.

Talk = function(){ return "hello " + this. Name; }; a.talk() // works be = new A("john"); b.talk() // works Here is what is going on: // Continued from above var old_proto = A. Prototype; // Nuke that proto A.

Prototype = { talk: function() { return "goodbye " + this. Name; } }; var c = new A("Al"); a.talk() // hello brad b.talk() // hello john c.talk() // goodbye Al old_proto. Say_goodbye = function() { return "goodbye " + this.Name; }; a.

Say_goodbye() // goodbye brad b. Say_goodbye() // goodbye john c. Say_goodbye() // TypeError c.

Say_goodbye is not a function.

– brad Mar 3 at 19:00 @brad -- yes, that is exactly what is happening. – Sean Vieira Mar 3 at 19:03 @brad -- added a bit more explanation. – Sean Vieira Mar 3 at 19:10 1 makes perfect sense... thanks for the thorough explanation!

– brad Mar 3 at 19:27.

In support of Sean's good answer: there's nothing wrong with replacing the entire prototype, as long as you do it before you create instances of the object. This also works: function A(name){ this. Name = name; } A.

Prototype = { talk: function(){ return "hello " + this. Name; } } a = new A("brad"); a.talk() // works Just be sure not to replace it later (unless that's what you're trying to do). In your original example A didn't have your custom prototype at the time you created your first instance, but it did have it when you created your second one, because you created the prototype in between.

The prototype chain is established when an object is instantiated, so it's possible for two instances of the same "class" to have different prototypes, as you demonstrated. This can cause all kinds of trouble: var a = new A("brad"); console. Log(a instanceof A) // true A.

Prototype = { talk: function(){ return "hello " + this. Name; } } console. Log(a instanceof A) // false The object referred to by a is no longer considered an instance of A because instanceof works by checking if A.

Prototype is in the prototype chain of a.

Ya I'm aware of the ordering issues, I wasn't however aware of the difference in replacing vs modifying of the prototype. Thanks for the extra clarification though – brad Mar 3 at 19:10 1 To word it a bit differently even, when you create a new object instance with "new", the following happens: 1. ) Create an empty object and run the constructor in the scope of that object.2.) Set the new object instance's prototype property to the constructor's prototype property.

Thus as lwburk has said, when the prototype is set, A. Prototype is something else. – XHR Mar 3 at 19:11.

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