EXT learning Notes II (class definition)

Source: Internet
Author: User

Use the following statement in ext to define a class,

 

  1. 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.

 

  1. Ext. Apply (person. prototype ,{
  2. Name: "Chris Mao", // custom attributes
  3. Print: function () {// custom Method
  4. Alert (this. Name );
  5. }
  6. });

Use namespace
Anyone familiar with object-oriented programming knows the namespace concept. Ext also supports namespaces.

 

  1. Ext. namespace ("Ext. Emerson"); // defines the namespace
  2. Ext. Emerson. Person = ext. emptyfn;
  3. Ext. Apply (ext. Emerson. Person. prototype ,{
  4. Name: "Chris Mao", // custom attributes
  5. Print: function () {// custom Method
  6. Alert (this. Name );
  7. }
  8. });

Example:

  1. <HTML>
  2. <Head>
  3. <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
  4. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
  5. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
  6. <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
  7. <SCRIPT type = "text/JavaScript">
  8. VaR P = new Ext. Emerson. Person ();
  9. P. Name = 'chris Mao '; // attribute assignment
  10. P. Print (); // call the print () method
  11. </SCRIPT>
  12. </Head>
  13. <Body>
  14. </Body>
  15. </Html>

Define Constructors

  1. Ext. namespace ("Ext. Emerson"); // defines the namespace
  2. Ext. Emerson. Person = function (_ CFG ){
  3. // Do something
  4. Ext. Apply (this, _ CFG );
  5. };
  6. Ext. Apply (ext. Emerson. Person. prototype ,{
  7. Print: function () {// custom Method
  8. Alert (this. Name );
  9. }
  10. });

Example

  1. <HTML>
  2. <Head>
  3. <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
  4. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
  5. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
  6. <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
  7. <SCRIPT type = "text/JavaScript">
  8. VaR P = new Ext. Emerson. Person ({name: "Chris Mao "});
  9. P. Print (); // call the print () method
  10. </SCRIPT>
  11. </Head>
  12. <Body>
  13. </Body>
  14. </Html>


Define static methods

  1. Ext. Emerson. Person. Print = function (_ name ){
  2. VaR P = new Ext. Emerson. Person ();
  3. P. Name = _ name;
  4. P. Print ();
  5. }

Example

  1. <HTML>
  2. <Head>
  3. <LINK rel = "stylesheet" type = "text/CSS" href = "http: // plt385130: 8080/ext-2.2/resources/CSS/ext-all.css"/>
  4. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/adapter/EXT/ext-base.js"> </SCRIPT>
  5. <SCRIPT type = "text/JavaScript" src = "http: // plt385130: 8080/ext-2.2/ext-all.js"> </SCRIPT>
  6. <SCRIPT type = "text/JavaScript" src = "person. js"> </SCRIPT>
  7. <SCRIPT type = "text/JavaScript">
  8. VaR P = new Ext. Emerson. Person ();
  9. // Call the static method
  10. Ext. Emerson. Person. Print ('mao zibing ');
  11. </SCRIPT>
  12. </Head>
  13. <Body>
  14. </Body>
  15. </Html>

Class inheritance

Defines a student class, inherits from the person class, and adds the job attribute.

  1. Ext. Emerson. Student = function (_ CFG ){
  2. // Do something
  3. Ext. Apply (this, _ CFG );
  4. };
  5. Ext. Extend (ext. Emerson. Student, ext. Emerson. Person, {job: "student "});

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.