Classes inheritance problem

S

Sergey Ilinsky

Hi everybody.

I've discovered the following problem while building an APP:

/* Code Start **************************/
// Definition
function cClass_prototype_prototype()
{
this.array = new Array;
this.className = "cClass_prototype_prototype";
}

function cClass_prototype()
{
this.className = "cClass_prototype";
}

cClass_prototype.prototype = new cClass_prototype_prototype;

function cClass1()
{
this.className = "cClass1";
}

function cClass2()
{
this.className = "cClass2";
}

cClass1.prototype = new cClass_prototype;
cClass2.prototype = new cClass_prototype;

oClass1 = new cClass1();
oClass2 = new cClass2();

// Testing

alert(oClass1.array)
alert(oClass2.array)

oClass1.array.push(1);

alert(oClass1.array)
alert(oClass2.array)

/* Code End ****************************/

If you will execute this code you will see that pushing an value into
the first class instance array property cause changing value of the
second class instance array property.
Is it possible to have array values not connected to each other?
I would like to avoid defining "array" property in cClass1 and cClass2
(in this case it would work the proper way) and have them defined only
in third-level parent class cClass_prototype_prototype.
 
M

Martin Honnen

Sergey Ilinsky wrote:

If you will execute this code you will see that pushing an value into
the first class instance array property cause changing value of the
second class instance array property.

Yes, but that is simply because neither the "first class instance" nor
the "second class instance" have an own property named array but only up
in their common prototype chain there is an object with a property named
array (which indeed is an Array) and this array is filled.
Is it possible to have array values not connected to each other?
I would like to avoid defining "array" property in cClass1 and cClass2
(in this case it would work the proper way) and have them defined only
in third-level parent class cClass_prototype_prototype.

If you want different properties for instances then make sure each
instance gets its own property, a prototype makes mostly sense for
function properties (methods) and for properties which are meant to be
shared between instances (a bit similar to static properties in Java or C#).
 
M

michael elias

I use inheritance in javscript like this:

function BaseClass(){

}

Class1.prototype = new BaseClass;
Class1.prototype.constructor = Class1 // optional
function Class1(){
BaseClass.call(this);
}

this way all properties of an inherited class are incapsulated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top