-
9 Aug 2011 5:51 AM #11
This seems like a fairly significant thing to me.
I don't know that it's "simply how JavaScript prototypal inheritance work" especially since Ext is controlling it by adding everything to the prototype of the class instead of the individual instance. As it stands right now it appears to be automatically treating any object or array defined at the class level as a static. However, you've given the ability to define "statics" so I don't know why you wouldn't leverage that configuration when dealing with any property, including arrays/objects.
Is this something that Sencha would be willing to reconsider?
-
9 Aug 2011 7:48 AM #12
I agree that this is indeed pretty puzzling for programmers coming from a different background and applying classic OOP principles and terms to prototype-based inheritance.
However, in Javascript the only(?) way to allow a property to be inherited is to add it to the prototype.
Javascript handles resolving properties on an object (like obj.property) and traverses the prototype chain to find a match if the object itself does not define it (result: inheritance)
Any object can override the property by re-assigning a different value (this.property=..). The property will then be owned by that object alone.
It has been pointed out in this thread before, that for immutable values the behavior appears to be that of classic-OOP class fields.
The confusion starts once you do not *reassign* (override) the property value but manipulate/modify it.
How could Ext provide a solution to this?
Looking at classic OOP and compiled languages (or languages that have the construct of a 'class'), you have to consider that the code statement that defines the (default) value for a class field is re-evaluated each time an instance of the class is created.
This is not possible in Javascript and in Ext.define(). The code statement that defines the value for the property will be evaluated exactly once and the *result* will be passed to Ext.define! The class object that Ext creates from the class definition cannot really imitate the behavior described above.
One suggestion was to clone the object and assign a copy to every object that is created as instance of this class. But as I pointed out in my previous post, cloning only works for simple objects.
hth
-
9 Aug 2011 10:16 AM #13
That does make sense... even if I'm not happy about it...

I can see how simply always doing a clone wouldn't necessarily be a good idea, especially if one of those properties happened to be a reference to some other instance.
Well, I'm glad I came across this thread. And thanks for the detailed explanation. That could have made for some especially insidious bugs down the road.
-
9 Aug 2011 10:55 AM #14Sencha - Sencha Touch Dev Team
- Join Date
- Jul 2009
- Location
- Palo Alto, California
- Posts
- 469
- Vote Rating
- 9
Compare these 2 pieces of code:
Code:Ext.define('Foo', { bar: { baz: true }, constructor: function() { this.bar = Ext.clone(this.self.prototype.bar); } });You will immediately find that it doesn't make sense sacrificing (huge) performance to clone to original object for every instance of Foo, instead of just creating a new one inside the class' constructor.Code:Ext.define('Foo', { constructor: function() { this.bar = { baz: true }; } });Sencha Touch Lead Architect
Looks like we can't reproduce the issue or there's a problem in the test case provided.


Reply With Quote