Ext中类的使用
来源:互联网 发布:statnba数据 编辑:程序博客网 时间:2024/06/05 01:42
1、定义一个类
- //使用Ext定义一个类
- Ext.define('Person',{
- name:'jaune',
- age:18
- });
- //创建一个类
- var person = new Person();
- console.log(person);
从打出来的结果中可以看出我们定义的属性已经出现在了类中,这是Ext中最简单的类的定义方式。
注意上图中的superclass,我们用Ext定义的类如果没有指明继承自哪个类,默认都是继承自Ext.Base,
这个类就相当于Java中的Object类,是Ext所有类的基类。如何继承其他类会在下面讲到。
2、Ext中构造函数
- Ext.define('Person',{
- name:'jaune',
- age:18,
- constructor:function(config){
- Ext.apply(this,config);
- }
- });
- //创建一个类
- var person = new Person({
- name:'petty'
- });
- console.log(person);
3、类的继承
- //使用Ext定义一个类
- Ext.define('Person',{
- name:'jaune',
- age:18,
- constructor:function(config){
- Ext.apply(this,config);
- }
- });
- //类的继承
- Ext.define('Man',{
- extend:'Person',
- sex:'Male',
- constructor:function(config){
- //这里是为了确保创建的对象中的sex属性是Male,如果config中有sex属性就删除这个属性
- if(config.hasOwnProperty('sex')){
- delete config.sex;
- }
- /*
- * callParent的意思就是调用父类同名的方法,这里用作继承父类构造方法
- * 比如父类中有一个toString方法,在子类的toString方法中调用this.callParent()方法,则会执行父类中的toString方法
- * 这个大家可以亲自验证
- */
- this.callParent([config]);
- },
- //这个方法是为了方便打印
- toString:function(){
- return {
- name:this.name,
- age:this.age,
- sex:this.sex
- };
- }
- });
- var man = new Man({
- name:'tom',
- sex:'Female'
- });
- console.log(man.toString());
- /*
- * 打印结果如下
- * Object {name: "tom", age: 18, sex: "Male"}
- */
然后调用父类的构造方法将config中的属性赋予man对象
4、类的静态属性和静态方法
- /**
- * statics 可以包含类的静态和静态方法,但是不能被子类继承
- * inheritableStatics 与statics类似但是可以被子类继承
- */
- Ext.define('DateUtil',{
- inheritableStatics:{
- currentDate:Ext.Date.format(new Date(),'Y-m-d'),
- getCurrentDate:function(formatStr){
- if(Ext.isString(formatStr)){
- Ext.Date.format(new Date(),formatStr);
- }else{
- return this.currentDate;
- }
- }
- }
- });
- console.log(DateUtil.currentDate);
- Ext.define('TimeUtil',{
- extend:'DateUtil',
- statics:{
- currentTime:Ext.Date.format(new Date(),'Y-m-d H:i:s')
- }
- });
- console.log(TimeUtil.currentDate);
上面的两个类,如果DateUtil中用的是statics属性来定义静态属性和方法则TimeUtil无法继承
5、单例
在Ext中定义单例模式的类非常简单,在定义类的时候加上singleton:true 就表示要定义的类为单例,剩下的事情Ext会替你 解决,只需要像定义普通类一样编码就可以了。
- Ext.define('DateUtil',{
- singleton:true,
- currentDate:Ext.Date.format(new Date(),'Y-m-d'),
- getCurrentDate:function(formatStr){
- if(Ext.isString(formatStr)){
- Ext.Date.format(new Date(),formatStr);
- }else{
- return this.currentDate;
- }
- }
- });
- console.log(DateUtil);
JS中的单例其实就是将类实例化,可能没有大家想的那么复杂
阅读全文
0 0
- Ext中类的使用
- EXT中 使用的 highcharts
- Ext中关于Function类的一些新方法的使用
- 使用Ext.util.TaskRunner类实现,Ext的API中有示例,grid页面自动刷新
- Ext中关于Ext.QuickTips.init()的使用
- Ext中关于Ext.QuickTips.init()的使用
- Ext中关于Ext.QuickTips.init()的使用
- Ext中关于Ext.QuickTips.init()的使用
- Ext中关于Ext.QuickTips.init()的使用
- Ext中关于Ext.QuickTips.init()的使用
- ExtJS教程(3)--- Ext中类的使用
- ext js 中combo对象的使用
- Ext Js 3.2中GroupView的使用
- ext中对RowExpander的使用
- ext中使用iframe的tabs页
- ext中类的继承
- 【Ext.Net学习笔记】04:Ext.Net中使用数据、Ext.Net Store的用法、Ext.Net ComboBox用法
- Ext 中使用Ajax
- Junit测试问题:java.lang.Exception: No tests found matching
- 参加首届“全球程序员节”感想
- Spring容器中Bean的作用域
- 性能测试内容总结
- 在整合hibernate时报异常
- Ext中类的使用
- 基于ajax、jquery、js制作的智能搜索框的
- redis数据库命令快查表
- python自动化测试应用-第10篇-python调用安卓adb命令(上篇)
- hdfs fsck命令查看HDFS文件对应的文件块信息(Block)和位置信息
- OpenStack不影响业务的情况下,动态减少巨页内存
- iOS开发 解决Xcode9的Log日志无法输出中文的问题
- 动态设置view或布局的宽高
- 数据结构----顺序栈