By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Classes inheritance problem

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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#).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.