This project is archived and is in readonly mode.

#840 ✓invalid
Roman

Using non static class veriables

Reported by Roman | January 31st, 2010 @ 09:10 PM | in 1.3.0 rc2 (closed)

Hi,

It would seem that by using an initialized class as a veriable for another class, like so:

var MyClass = new Class({

instVar = new Hash()

})

Will get the 'instVar' to be shared by instances of of MyClass.. this behaviour caused a bug in development and it appears to me that other people share my misunderstanding of this behaviour..

Would love to see this mentioned somewhere in the Class documentation.

Cheers,
-- Roman

Comments and changes to this ticket

  • Fábio M. Costa

    Fábio M. Costa February 6th, 2010 @ 05:34 PM

    Hi Roman, thanks for sharing.

    Your code looks invalid. But i think i understood the problem, just don't know why you would create 2 classes with the same content?

  • Roman

    Roman February 6th, 2010 @ 07:52 PM

    Hi Fabio,

    Did you mean my code looks valid? :)

    Please have a look at the relevant thread here:
    http://groups.google.com/group/mootools-users/browse_thread/thread/...

    My point is that according to Aaron, mootools is using Object.reset function to "clean" the class we initializing inside the Class constructor, but looking at Object.clean I see it only affects types of array and object, otherwise when I do this:
    MyClass = new Class({
    myHash : new Hash() }) It will do nothing.. thus as a result every new instance of MyClass will (!!)share the pointer to 'myHash', not knowing this caused bugs for me, so I figure it needs some clarification for the Class documentation so developers will know what to expect..

    I couldnt say I am super in JS so if I havent made some point clean please let me know!

    Cheers

  • Fábio M. Costa

    Fábio M. Costa February 7th, 2010 @ 11:03 PM

    • Assigned user set to “Christoph Pojer”

    No, it was really invalid but of course we understand what you wanna show.

    So yeah it looks like there should be a check for the hash type, why dont we just use $unlink here anyway?
    Lets see what cpojer says.

  • Roman

    Roman February 7th, 2010 @ 11:32 PM

    Ahh, your right the equal assignment errr, sorry, working too many hours to get this live.. :)

    I would vote for $unlink.

    Nice week!
    -- Roman

  • fakedarren

    fakedarren February 8th, 2010 @ 12:43 PM

    • State changed from “new” to “open”
    • Milestone changed from 2.0 to 1.3.0 rc2

    Christoph, thoughts on this?

  • Roman

    Roman March 29th, 2010 @ 04:46 PM

    Any updates on this?

    Thanks! :)

  • Fábio M. Costa

    Fábio M. Costa March 30th, 2010 @ 12:32 AM

    This is the same problem as this ticket:

    https://mootools.lighthouseapp.com/projects/2706/tickets/873-hash-d...

    Theres a way to avoid this problem there.

  • Roman

    Roman March 30th, 2010 @ 01:45 AM

    Thanks for the follow-up!

    Could you please elaborate why this is the correct behavior? I assume many will make the same mistake I did expecting the Class constructor to behave as a constructor.. so I would appreciate if you could help me understand this..

    Cheers,
    Roman

  • Christoph Pojer

    Christoph Pojer March 30th, 2010 @ 02:07 AM

    • State changed from “open” to “invalid”

    Even though we consider Hash a "Type" or "Native", if we allow for that behavior people might also ask why it is not possible to clone an instance of a class in the correct way (via a copy constructor or something). JavaScript itself does not really differentiate between what is a Hash instance and a Class instance, we just make everyone believe there is a difference to make working with JavaScript easier :) Note that when adding something natively to the prototype of an object like MyObject.prototype.myMember this will only work for primitive values like Number or String but not for objects or arrays. MooTools helps you here by cloning objects and arrays.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages