PDA

View Full Version : problems with Ext.extend



nickevin
21 Jul 2009, 10:51 PM
[CODE]

var Person = function (config) {
this.name = 'foo';
this.toString = function () {
return 'form Person';
}
}

var Student = function (config) {
Ext.apply(this, config);
Student.superclass.constructor.call(this, {
name: config.name
});
}

Ext.extend(Student, Person, {
toString: function () {
return 'form Student';
}
});

var s = new Student({
name: 'bar'
});

[COLOR=

danh2000
21 Jul 2009, 11:17 PM
Ext.extend extends the prototype not the instance.

If you are going to get confused between instance and prototype, extend all the way up from object:


var Person = function (config) {
Ext.apply(this,config);
}
Ext.extend(Person,Object,{
toString: function(){
return 'from Person';
}
});
var Student = function (config) {
Student.superclass.constructor.call(this, config);
}

Ext.extend(Student, Person, {
toString: function () {
return 'form Student';
}
});

var p = new Person({
name: 'foo'
});
var s = new Student({
name: 'bar'
});
alert(p.name);
alert(p.toString());
alert(s.name);
alert(s.toString());

nickevin
21 Jul 2009, 11:41 PM
thx to danh2000!!

the code is good . and i modify your code .

[CODE]

var Person = function (config) {
Ext.apply(this, config);
}
Ext.extend(Person, Object, {
toString: function () {
return 'from Person';
}
});
var Student = function (config) {
Student.superclass.constructor.call(this, config);
}

Ext.extend(Student, Person, {});

var p = new Person({
name: 'foo'
});
var s = new Student({
// [COLOR=

Lukman
22 Jul 2009, 2:48 AM
s doesn't extend p.

It is Student that extends Person.

Like danh2000 said, you are getting confused between instance and prototype. s and p are instances. Student and Person are prototype. [name: 'foo'] goes to p, it doesn't go to the Person protoype, so when you instantiate s as a Student, it doesn't get the name value. If you want Student to get [name: 'foo'] by default when instantiated, put the config into Student prototype, or Person prototype.