On Fri, Mar 12, 2010 at 05:54, skar <[email protected]> wrote:

> Hi,
>
> I've got a class like this:
>
> qx.Class.define("testproject.ArrayClass",
> {
>  extend: qx.ui.basic.Atom,
>
>  construct : function()
>  {
>    arguments.callee.base.apply(this);
>    this.arr2 = new Array();
>  },
>  members : {
>    arr : new Array()
>   }
> });
>
> Now, I do the following:
>
> s1=new testproject.ArrayClass();
> s2=new testproject.ArrayClass();
> s1.arr==s2.arr -> evaluates to true
> s1.arr2==s2.arr2 -> evaluates to false
>
> Now pushing any object/value into s1.arr results in s2.arr also having
> the same values. Both the arrays point to the same array values. Why
> does arr2 result in per instance arrays while arr has the same array
> being shared across all the instances?
>

Yes, this is a known issue, but one that is easy to resolve in your code.
The problem is that if you provide an initial value to a member variable,
and that initial value is a reference type (as is an array), then all
instantiations of that object will share the reference.

To solve the problem, simply change your members section to read like this:

 members : {
   // arr : new Array()      can't do this because Array() provides a
reference
  arr : null
  }

You're creating a new Array for each instance in the constructor, which is
correct.

As a side note:
   arguments.callee.base.apply(this);
is typically in qooxdoo, these days, written as:
  this.base(arguments);

Cheers,

Derrell
-- 
"There are two ways of constructing a software design.
One way is to make it so simple that there are obviously no deficiencies.
And the other way is to make it so complicated that there are no obvious
deficiencies."

                                               C.A.R Hoare
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to