ExtJS Learning------ext.define Inheritance Extend, using JavaScript to achieve an inheritance like EXT

Source: Internet
Author: User

(1) Succession of Ext.define extend

Specific examples:

Ext.onready (function () {//sup class parent ext.define (' person ', {config:{name: ' BJSXT '}, constructor:function (config) {var me = this; Me.initconfig (config);}); /sub class Subclass Ext.define (' Boy ', {//using EXT's inheritance Extend: ' person ',//Direct inheritance Config:{sex: ' Male ', age:20}}), var B = ext.create (' Boys ', { Name: ' Zhang San ', age:25}); alert (' Name: ' +b.name+ '--Gender: ' +b.sex+ '--Age: ' +b.age ');});

Example Results:


(2) Use JavaScript achieve similar Ext The Inheritance

Instance:

Ext.onready (function () {//javascript:prototype (prototype): implementation inherits//supclassvar person = function (name) {this.name = name;};// alert (Person.prototype.constructor);//constructor of the prototype object, default is the template of the current class//supclass prototype Objectperson.prototype = { Constructor:person, Id:100};//subclassvar boy = function (name,sex,age) {//How to borrow constructor inheritance Person.call (This,name); This.sex = sex; this.age = age;};/ /Implement prototype Inheritance: inherits the template of the parent class and the prototype object of the parent class//boy.prototype = new Person ();//Implement Extend Method function Myextend (sub, sup) {var F = Functi On () {},//defines an empty function as a prototype object of the subclassproto,//subclass of the relay function//The prototype object of the parent class is given the Superclassproto variable Superclassproto = Sup.prototype;        The location of the relay: The prototype object of the parent class is assigned to the prototype object of the empty function of f//prototype inheritance f.prototype = Superclassproto;        Subclassproto = Sub.prototype = new F ();  Subclassproto.constructor = sub;//Restore Constructor Sub.superclass = superclassproto;//do a save, save the prototype object of the parent class//The purpose is to prevent you from careless if (Superclassproto.constructor = = = Object.prototype.constructor)       {superclassproto.constructor = sup; }};myextend (boy, person);//implementation of the method of inheritance var B = new boy (' John Doe ', ' Male ', 25);///* Note: The traditional JavaScript method implements inheritance * boy.prototype=new person ( ' John Doe '); * var b=new boy (' Male ', 25); */alert (' Name: ' +b.name+ '--Gender: ' +b.sex+ '--id: ' +b.id+ '--Age: ' +b.age ');

Example Results:


Related Article

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.