『ExtJS』01 001. ExtJS 4 类的定义

本文详细介绍了如何使用ExtJS4定义类、配置属性、添加方法及实例化类的过程,并通过示例代码展示了类配置、方法调用及属性操作。包括自动生成属性相关方法和使用config参数进行属性配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExtJS 4 类的定义

类的定义与类方法的调用


样例代码

// Define new class 'Vehicle' under the 'Cookbook' namespace
Ext.define('Cookbook.Vehicle', {
    // class configuration goes here
    Manufacturer : 'Aston Martin',
    Model : 'Vanquish',

    getDetails : function() {
        alert('I am an ' + this.Manufacturer + ' ' + this.Model);
    }
}, function(){
    Console.log('Cookbook.Vehicle class defined!');
});

var myVehicle = Ext.create('Cookbook.Vehicle');
alert(myVehicle.Manufacturer);
myVeicle.getDetails();

说明

  • 第二行,使用Ext.define定义了一个Cookbook.Vehicle类,Cookbook是命名空间。
  • 第四行、第五行,加入了两个属性。
  • 第七行,加入了一个getDetails方法。
  • 第十行,加入了callback方法,这个方法将在类定义之后执行。
样例代码2// Define new class 'Vehicle' under the 'Cookbook' namespace
Ext.define('Cokbook.Vehicle', {
    config : {
        // class configuration goes here
        Manufacturer : 'Aston Martin',
        Model : 'Vanquish',
    },

    constructor : function(config) {
        // initialise our config object
        this.initConfig(config);
    },

    getDetails : function() {
        alert('I am an ' + this.Manufacturer + ' ' + this.Model);
    }
}, function() {
    Console.log('Cookbook.Vehicle class defined!');
});

// create a new instance of Vehicle class
var myVehicle = Ext.create('Cokbook.Vehicle');
// display its details
myVeicle.getDetails();
// update Vehicle details
vehicle.setManufacturer('Volkswagen');
vehicle.setModel('Golf');
// display its new details
vehicle.getDetails();

 

说明

  • 如果在类的定义内,使用config标明配置属性,则Ext会自动生成属性相关的set、get、apply方法。
样例代码3Ext.define('Cookbook.Vehicle', {
    Manufacturer : 'Aston Martin',
    Model : 'Vanquish',

    getManufacturer : function() {
        return this.Manufacturer;
    },
    setManufacturer : function() {
        this.Manufacturer = value;
    },
    resetManufacturer : function() {
        this.setManufacturer('Aston Martin');
    },
    applyManufacturer : function(manufacturer) {
        // perform some action to apply the value(e.g. update a DOM element
        return manufacturer;
    },
    getModel : function() {
        return this.Model;
    },
    setModel : function(value) {
        this.Model = value;
    },
    resetModel : function() {
        this.setModel('Vanquish');
    },
    applyModel : function(model) {
        // perform some action to apply the value (e.g. update a DOM element)
        return model;
    },
    getDetails : function() {
        alert(' ... ')
    }
});

总结


  1. 使用Ext.define()方法定义类主要有三个部分:类配置部分,类方法,定义回调方法。
  2. ExtJS中,定义、实例化一个类时,直接使用字符串的命名的形式。

转载于:https://www.cnblogs.com/sitemanager/archive/2012/11/23/2784634.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值