Use the following statement in ext to define a class,
- Person = ext. emptyfn;
Of course, the class defined in this way is just an empty shelf, without any attributes and Methods. Use the following code to add attributes and methods for the class.
- Ext. Apply (person. prototype ,{
- Name: "Chris Mao", // custom attributes
- Print: function () {// custom Method
- Alert (this. Name );
- }
- });
Use namespace
Anyone familiar with object-oriented programming knows the namespace concept. Ext also supports namespaces.
- Ext. namespace ("Ext. Emerson"); // defines the namespace
- Ext. Emerson. Person = ext. emptyfn;
- Ext. Apply (ext. Emerson. Person. prototype ,{
- Name: "Chris Mao", // custom attributes
- Print: function () {// custom Method
- Alert (this. Name );
- }
- });
Example:
- <HTML>
- <Head>
- <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript">
- VaR P = new Ext. Emerson. Person ();
- P. Name = 'chris Mao '; // attribute assignment
- P. Print (); // call the print () method
- </SCRIPT>
- </Head>
- <Body>
- </Body>
- </Html>
Define Constructors
- Ext. namespace ("Ext. Emerson"); // defines the namespace
- Ext. Emerson. Person = function (_ CFG ){
- // Do something
- Ext. Apply (this, _ CFG );
- };
- Ext. Apply (ext. Emerson. Person. prototype ,{
- Print: function () {// custom Method
- Alert (this. Name );
- }
- });
Example
- <HTML>
- <Head>
- <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript">
- VaR P = new Ext. Emerson. Person ({name: "Chris Mao "});
- P. Print (); // call the print () method
- </SCRIPT>
- </Head>
- <Body>
- </Body>
- </Html>
Define static methods
- Ext. Emerson. Person. Print = function (_ name ){
- VaR P = new Ext. Emerson. Person ();
- P. Name = _ name;
- P. Print ();
- }
Example
- <HTML>
- <Head>
- <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript">
- VaR P = new Ext. Emerson. Person ();
- // Call the static method
- Ext. Emerson. Person. Print ('mao zibing ');
- </SCRIPT>
- </Head>
- <Body>
- </Body>
- </Html>
Class inheritance
Defines a student class, inherits from the person class, and adds the job attribute.
- Ext. Emerson. Student = function (_ CFG ){
- // Do something
- Ext. Apply (this, _ CFG );
- };
- Ext. Extend (ext. Emerson. Student, ext. Emerson. Person, {job: "student "});