Classes inheritance problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergey Ilinsky

    Classes inheritance problem

    Hi everybody.

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

    /* Code Start *************** ***********/
    // Definition
    function cClass_prototyp e_prototype()
    {
    this.array = new Array;
    this.className = "cClass_prototy pe_prototype";
    }

    function cClass_prototyp e()
    {
    this.className = "cClass_prototy pe";
    }

    cClass_prototyp e.prototype = new cClass_prototyp e_prototype;

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

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

    cClass1.prototy pe = new cClass_prototyp e;
    cClass2.prototy pe = new cClass_prototyp e;

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

    // Testing

    alert(oClass1.a rray)
    alert(oClass2.a rray)

    oClass1.array.p ush(1);

    alert(oClass1.a rray)
    alert(oClass2.a rray)

    /* 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_prototyp e_prototype.
  • Martin Honnen

    #2
    Re: Classes inheritance problem



    Sergey Ilinsky wrote:

    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > 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_prototyp e_prototype.[/color]

    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

    Comment

    • michael elias

      #3
      Re: Classes inheritance problem

      I use inheritance in javscript like this:

      function BaseClass(){

      }

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

      this way all properties of an inherited class are incapsulated.

      Comment

      Working...