1、类的2中定义和3中实例化方式
- Ext.onReady(function() {
- //自定义类的2中方法
- Ext.define("person",{
- extend : "Ext.data.Model",
- fields:[
- {name:'name',type:'auto'},
- {name:'age',type:'int'},
- {name:'email',type:'auto'}
- ]
- });
- Ext.regModel("user",{
- fields:[
- {name:'name',type:'auto'},
- {name:'age',type:'int'},
- {name:'email',type:'auto'}
- ]
- });
- //instance三种方法
- //method 1
- var p1 = new persion({
- name : 'zhangsan',
- age : 12,
- email : 'tjwkj@sina.com'
- });
- alert(p1.get('name'));
- //method 2
- var p2 = Ext.create("persion",{
- name : 'zhangsan',
- age : 12,
- email : 'tjwkj@sina.com'
- });
- alert(p2.get('name'));
- //method 3
- var p3 = Ext.ModelMgr.create({
- name : 'zhangsan',
- age : 12,
- email : 'tjwkj@sina.com'
- });
- alert(p3.get('email'));
- });
2、自定义类、事件机制
- Ext.onReady({
- Ext.define("children",{
- extend : 'Ext.util.Observable',
- constructor : function(){
- this.state = "hungry",
- this.setMilk = function(milk){
- this.fireEvent('hungry',milk);
- },
- this.addEvents('hungry':true),
- this.addListener('hungry',function(milk){
- if(this.state == 'hungry'){
- this.drink(milk);
- }else{
- Ext.Msg.alert("no hungry");
- }
- }),
- this.drink = function(milk){
- alert("eat one bottle of milk.");
- }
- }
- });
- var children = Ext.create("children",{});
- children.setMilk("三鹿牛奶");
- });
3、常规的javascript定义类及访问权限
- function user() {
- //public
- this.name = 'uspcat';
- this.age = 20;
- //private
- var email = "tjwkj@sina.com";
- //通过方法提供private的访问
- this.getEmail = function() {
- return email;
- }
- }
- var u = new user();
- alert(u.name);
- alert(u.age);
- alert(u.getEmail());
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/816777,如需转载请自行联系原作者