PDA

View Full Version : Ext.define



kyousuke
10 Sep 2011, 11:46 PM
Ext.define("com.yx.MyObject", {
name: "Kotori"
});
var g = Ext.create("com.yx.MyObject", {
name: "Nagisa"
});
alert(g.name);

The result is "Kotomi". Why it can't over write the name attribute.
And I want to do something like this.


Ext.define("com.yx.MyObject", {
name: "Kotori"
subObj: {
otherName: "Nagisa"
}
});
var g = Ext.create("com.yx.MyObject",{
subObj: {
otherAttr: "Tomoyo"
}
});
alert(g.subObj.otherName);
alert(g.subObj.otherAttr);

I want the result is "Nagisa" and "Tomoyo", but it is not what I want.
How to do it.

skirtle
11 Sep 2011, 7:44 AM
The reason why properties specified in the config usually end up on the instance created is that most classes have a constructor that does this:


Ext.apply(this, config);

The class you have defined here doesn't do that, so the config object is just being ignored.

As for your second case, merging the objects like that is both difficult and dangerous. You can easily end up accidentally mutating objects that are on the prototype. It is possible to do it correctly but you'd have to do all the work manually in your constructor.