Instance member working as static

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Instance member working as static

tashos
Hi,
We are using qooxdoo for development and I experienced a bug. I have
declared a member that is pointing to a object and edit that member in
the constructor of the class. When I create two or more instances of
said class the member always points to the last created instance. If I
assign the member to "this" like "this.member = {};" then it works like
it is expected to. Can you give me a suggestion or a reason as to why
this is happening?

PS.: The declaration in members goes as follows: members:
{
     member: {},
     funcUsingMember: function () {
         // Some business logic

         return this.member.key;
     }
}


------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: Instance member working as static

Derrell Lipman
Hi Tashos. This is just how JavaScript works. An object (map) is a "reference type' in JavaScript. That means that regardless of how many variables are assigned that same value, they'll all get a reference to the one and only actual object. So when you create your object in the members section like this:

...
members :
{
  x : {}
},
...

you are, at that very moment, creating the empty object and stating that as each instance of the object is created, the x member should "point to" (reference, in JavaScript parlance) that one single object.

Your solution of initializing the member in the constructor is the correct one.

Cheers,

Derrell


On Wed, Dec 16, 2015 at 10:53 AM tashos <[hidden email]> wrote:
Hi,
We are using qooxdoo for development and I experienced a bug. I have
declared a member that is pointing to a object and edit that member in
the constructor of the class. When I create two or more instances of
said class the member always points to the last created instance. If I
assign the member to "this" like "this.member = {};" then it works like
it is expected to. Can you give me a suggestion or a reason as to why
this is happening?

PS.: The declaration in members goes as follows: members:
{
     member: {},
     funcUsingMember: function () {
         // Some business logic

         return this.member.key;
     }
}


------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

------------------------------------------------------------------------------

_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: Instance member working as static

voger
This is also noted in the manual.
http://manual.qooxdoo.org/current/pages/core/classes.html#instance-members

Take a look at the link for more details and the preferred way to
initialize instance variables.

On 16/12/2015 06:07 μμ, Derrell Lipman wrote:

> Hi Tashos. This is just how JavaScript works. An object (map) is a
> "reference type' in JavaScript. That means that regardless of how many
> variables are assigned that same value, they'll all get a reference to
> the one and only actual object. So when you create your object in the
> /members/ section like this:
>
> ...
> members :
> {
>    x : {}
> },
> ...
>
> you are, at that very moment, creating the empty object and stating that
> as each instance of the object is created, the x member should "point
> to" (reference, in JavaScript parlance) that one single object.
>
> Your solution of initializing the member in the constructor is the
> correct one.
>
> Cheers,
>
> Derrell
>
>
> On Wed, Dec 16, 2015 at 10:53 AM tashos <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>     We are using qooxdoo for development and I experienced a bug. I have
>     declared a member that is pointing to a object and edit that member in
>     the constructor of the class. When I create two or more instances of
>     said class the member always points to the last created instance. If I
>     assign the member to "this" like "this.member = {};" then it works like
>     it is expected to. Can you give me a suggestion or a reason as to why
>     this is happening?
>
>     PS.: The declaration in members goes as follows: members:
>     {
>           member: {},
>           funcUsingMember: function () {
>               // Some business logic
>
>               return this.member.key;
>           }
>     }
>
>
>     ------------------------------------------------------------------------------
>     _______________________________________________
>     qooxdoo-devel mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>
>
>
> ------------------------------------------------------------------------------
>
>
>
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>


------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel