8. How to write classes in Ext.js
Ext core3.0 is used here. Ext.extend is used in Ext to define a class (of course it is more used to extend a class). Ext Various controls in the entire framework, such as Panel, MessageBox, etc., are extended using the Ext.extend method. Here we only use it to define the simplest class.
Looking at the code of Ext.extend, we can see that it still uses constructors and prototypes to assemble a class.
You only need to pass two parameters here. The first parameter is the root class Object, and the second is the prototype.
/**
* Person类
* @param {Object} name
*/
var Person = Ext.extend(Object,{
constructor : function(name) {this.name = name;},
setName : function(name) {this.name = name;},
getName : function() {return this.name;}
});
//Create an object
var p = new Person("Lily");
console.log(p.getName());//Lily
p.setName("Andy");
console.log(p.getName());//Andy
/ /Test whether instanceof and p.constructor correctly point to Person
console.log(p instanceof Person);//true
console.log(p.constructor == Person);//true
What is special is that if you simply define a class, then the first parameter can always be passed as Object.