`

Ext inheritance machnism

    博客分类:
  • Ext
阅读更多
Ext provides a utility function called Ext.extend (API reference) that is the mechanism for implementing class inheritance using the Ext framework. This gives you the ability to modify or extend the base functionality of any JavaScript class without making code changes directly to the class itself (this is also commonly referred to as subclassing, or inheriting from, a base class). It is the preferred method for extending Ext components.

To create a new class that inherits from an existing class, you first declare the new class constructor via a function, and then invoke the extend method to define the shared attributes of your new class. These shared attributes are generally methods, but if a data item may be shared between instances (i.e., similar to a static class variable in Java), it may be declared there too.

JavaScript does not provide a mechanism for automatic invocation of super class constructors, so you must call the super class explicitly from your constructor using the superclass property. The first argument should always be this to ensure that the constructor is executed with the scope of the calling function.

 

MyNewClass = function(arg1, arg2, etc) {
   // explicitly call the superclass constructor
   MyNewClass.superclass.constructor.call(this, arg1, arg2, etc); 
};
 
Ext.extend(MyNewClass, SomeBaseClass, {
  theDocument: Ext.get(document),
  myNewFn1: function() {
    // etc.
  },
  myNewFn2: function() {
   // etc.
  }
});
 

The following example is a real world extension to Ext to allow a resizable, draggable element to be constrained by a set of X, Y values that specify how far the object can be dragged in the horizontal and/or vertical planes.

// create constructor for new class
Ext.ResizableConstrained = function(el, config){
    Ext.ResizableConstrained.superclass.constructor.call(this, el, config);
};
 
// extend the base class
Ext.extend(Ext.ResizableConstrained, Ext.Resizable, {
    setXConstraint : function(left, right){
        // Obtain a reference to parent dd property and setXConstraint
        this.dd.setXConstraint(left, right);
    },
 
   setYConstraint : function(up, down){
     // Obtain a reference to parent dd property and setYConstraint
     this.dd.setYConstraint(up, down);
   }
});
 
// create an instance of the new class
var myResizable = new Ext.ResizableConstrained('resize-el', {
   width: 200,
   height: 75,
   minWidth:100,
   minHeight:50, 
   maxHeight:150, 
   draggable:true
});
 
// invoke the new methods
myResizable.setYConstraint(0,300);
myResizable.setXConstraint(0,300);
 

In English terms, you could read the above code generally as the following: "Ext.ResizableConstrained extends Ext.Resizable, and implements these methods..."

Calling a base class method

If you need to override a method but call the same method of the superclass you can do this as below:

 

MyClass = Ext.extend(Ext.SomeClass, {
    someFunction : function(arg1, arg2){
         // custom code
 
         // call base class
         MyClass.superclass.someFunction.call(this, arg1, arg2);
 
         // custom code
    }
);
 

Don't forget to pass this or it won't run in your object context.

分享到:
评论

相关推荐

    inheritance

    【标题】"inheritance"指的是在编程领域中的继承机制,特别是在面向对象编程(OOP)的概念中。继承是面向对象编程的一个核心特性,允许一个类(子类或派生类)从另一个类(父类或基类)继承属性和方法。这种设计模式...

    Inheritance

    《继承在Java编程中的应用与理解》 继承是面向对象编程中的核心概念之一,它允许开发者从已有的类(父类或超类)派生出新的类(子类或派生类),以此来实现代码的复用和扩展。在Java编程语言中,继承通过关键字`...

    ext面向对象和继承

    面向对象的核心概念包括类(Class)、对象(Object)、继承(Inheritance)和多态(Polymorphism)。在EXTJS中,类通常通过`Ext.extend()`方法来定义,它创建了一个新的类,并继承自指定的父类。例如: ```...

    Ext继承和扩展

    `Ext`是一个流行的JavaScript库,它提供了丰富的UI组件和强大的数据管理工具,同时也引入了一套完整的面向对象系统,其中包括类(Class)和继承(Inheritance)的概念。本文将深入探讨`Ext`中的继承和扩展机制。 在...

    inheritance---derived-class.rar_inheritance

    在C++编程语言中,继承(Inheritance)是面向对象编程的一个核心概念,它允许一个类(称为子类或派生类)从另一个类(称为基类或父类)继承特性。这种特性使得代码重用变得简单,同时也能实现多态性。"inheritance--...

    EC.zip_eC_inheritance

    How do you choose between inheritance and templates? Between templates and generic pointers? Between public and private inheritance? Between private inheritance and layering? Between function ...

    Advanced JavaScript (closures,prototype,inheritance)

    JavaScript,作为一种广泛应用于Web开发的脚本语言,其高级特性如闭包(closures)、原型(prototype)和继承(inheritance)是理解其精髓的关键。本文将深入探讨这些概念,帮助开发者更好地掌握JavaScript的核心。 ...

    ACE-inheritance

    ### ACE 类结构与继承关系详解 #### 概述 ACE(Adaptive Communication Environment)是一个广泛应用于网络编程的高性能异步通信框架,它提供了一系列高级、可重用的C++类库,旨在简化分布式系统的开发。...

    Topic 10 Inheritance.md

    Topic 10 Inheritance.md

    C 程序设计教学课件:CHAPTER 8 INHERITANCE.ppt

    这在【标题】"C++程序设计教学课件:CHAPTER 8 INHERITANCE.ppt"中被详细讲解。继承的主要优点包括代码重用和表达程序组件之间的自然关系。 【描述】中提到,通过继承,基类的代码会被派生类自动继承,避免了重复...

    Python库 | oop-ext-0.3.2.tar.gz

    Python的内置OOP特性包括类(class)、对象(instance)、继承(inheritance)、封装(encapsulation)和多态(polymorphism)。然而,"oop-ext"库旨在提供这些基础之外的增强和扩展,可能包括更高级的设计模式、元...

    package-and-inheritance.zip_inheritance

    在Java编程语言中,封装、继承和多态是面向对象编程的三大核心概念,它们为构建复杂的软件系统提供了坚实的基础。下面将详细讲解这些概念及其重要性。 **封装**是面向对象编程的基本原则之一,它涉及到将数据和操作...

    C程序设计教学课件:CHAPTER8INHERITANCE.pptx

    在CHAPTER8INHERITANCE.pptx中,主要讲解了C++中的继承及其相关知识点。 8.1 引入 继承的主要目的是促进代码重用和表达程序组件之间的自然关系。例如,可以创建一个`Vehicle`基类,然后派生出如`Car`, `SaloonCar`,...

    Chapter7_Java_Inheritance.rar_inheritance

    在Java编程语言中,继承是面向对象编程的一个核心特性,它允许一个类(子类或派生类)从另一个类(父类或基类)继承属性和行为。这个概念是第7章“Java继承”中重点讲解的内容。通过继承,我们可以创建具有共同特性...

    Canalization of Development and the Inheritance of Acquired Characters

    在探讨《渠化的发展与后天性状的遗传》这一主题时,我们首先需要了解文章的核心概念及其背景。本文由沃丁顿(Waddington)于1942年发表在《自然》杂志上,主要关注了两个核心议题:发展生物学中的“渠化”现象以及...

    spring-jpa-inheritance

    1. **单表继承(Single Table Inheritance)**:所有子类的数据都存储在同一个表中,通过一个特定的字段(通常是`@DiscriminatorColumn`注解标记的)来区分不同子类的对象。这种策略的优点是简单,但可能导致表结构...

    Java_programming_code_inheritance_Thread.rar_inheritance

    在Java编程中,继承是面向对象编程的一个核心概念,它允许一个类(子类)继承另一个类(父类)的属性和方法。这里的主题聚焦于Java中的线程(Thread)类的继承,通过这种方式来创建自定义线程。下面将详细讨论Java继承...

    La-POO-con-Java.rar_inheritance

    Basic manual of java for beginners with simple and easy descriptions. Inheritance, composition, collections, files, interfaces

    qt-all-class-inheritance-diagrams.rar_QT树状图_inheritance_qt 树状图_q

    这个"qt-all-class-inheritance-diagrams.rar"压缩包包含了QT框架中的所有类的继承关系图,对于理解和学习QT库的架构至关重要。下面将详细阐述QT的类继承体系和相关知识点。 首先,QT的核心在于其面向对象的设计,...

Global site tag (gtag.js) - Google Analytics