Problem extending class with javascript object prototype?

That's why you shouldn't have mutable values (particularly objects or arrays) on a prototype - the same value will be shared across all object instances and can be changed in any of them. Here you can avoid the problem by using Object. Create that won't call the constructor of B when creating the prototype: A.

Prototype = Object. Create(B. Prototype) Constructor of A should then call the constructor of B for each new object: function A() { B.

Call(this); } For browsers that don't support Object.create() you can emulate it as mentioned on javascript.crockford.com/prototypal.html.

That's why you shouldn't have mutable values (particularly objects or arrays) on a prototype - the same value will be shared across all object instances and can be changed in any of them. Here you can avoid the problem by using Object. Create that won't call the constructor of B when creating the prototype: A.

Prototype = Object. Create(B. Prototype); Constructor of A should then call the constructor of B for each new object: function A() { B.

Call(this); } For browsers that don't support Object.create() you can emulate it as mentioned on javascript.crockford.com/prototypal.html.

Values are assigned by reference, and since all instances of A use the same instance of B as their prototype, they all refer to the same 'B'. So this is exactly what's expected here. One way to solve this, is to add (for instance) a 'initialize' method the B 'class', which you could then call from within the A constructor.

You can also not use 'new B()' to define the prototype, and use Object. Create instead. Object.

Create does not call B's constructor, but you can then call the parent constructor from A. Function A() { B. Call(this); }.

I tried this, my second requirement doesn't work here... That is, a instanceof B == true – Software Enthusiastic Jun 29 at 10:37 Thanks it worked... – Software Enthusiastic Jun 29 at 11:20.

This is a part of prototypes in Javascript, I suggest you read this excellent thread.

Thanks for your suggestions... – Software Enthusiastic Jun 29 at 11:26.

If you want instances of A to have a local property obj, add the property to A, not to B function B(){ } function A(){ this. Obj = {}; // prints false console. Log(a.

Obj === b. Obj); //=> prints false a.obj. Name = "stackoverflow"; console.

Log(b.obj. Name); //=> undefined another way to have a local obj property for instances of A is to use a setter method in B's prototype: function B(){ B.prototype. SetObj = function(obj){ this.

Obj = obj; return this; } } //... a. SetObj({}). Name = "stackoverflow"; console.

Log(a.obj. Name); //=>prints stackoverflow console. Log(b.obj.Name); //=>undefined.

As I said, obj is property of B, which need to accessed from derived class A. Do you mean while deriving should I copy all the properties from B to A? – Software Enthusiastic Jun 29 at 10:49 If you derive obj from B in A, obj is equal to all instances of A (it's prototype being B).

That's how it works. You can however declare a method in B's prototype, setting this.obj. See my edits.

– KooiInc Jun 29 at 10:59 Thanks for your suggestions... – Software Enthusiastic Jun 29 at 11:27.

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