Classes inheritance problem

Discussion in 'Javascript' started by Sergey Ilinsky, Feb 7, 2005.

  1. 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.
     
    Sergey Ilinsky, Feb 7, 2005
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Feb 7, 2005
    #2
    1. Advertising

  3. 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.
     
    michael elias, Feb 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,200
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    854
    cppsks
    Oct 27, 2004
  3. Thomas Witkowski
    Replies:
    2
    Views:
    362
    James Kanze
    Aug 6, 2007
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,070
  5. Replies:
    12
    Views:
    448
    Paavo Helde
    Mar 17, 2008
Loading...

Share This Page