---
title: "Extjs Config和Mixins"
author: "ych0108"
date: "2014-03-05T13:23:05Z"
tags: ["EXT", "config", "mixins"]
categories: ["Web前端"]
slug: "extjs-config-mixins"
draft: false
lastmod: "2014-03-05T13:23:55Z"
id: 2026064
url: "https://ych0108.iteye.com/blog/2026064"
---

Extjs 4中，为类型系统引入了Config概念，Config就是配置项的意思，用{configItem1:value1...}表示，在对象构造的时候，会调用this.initConfig(config)将配置项初始化，每个配置项自动生成4个函数：get set reset apply。
    Mixins也是新概念，相当于调用Ext.apply(this,other)将other类中的方法合并到当前的类中，也相当于另一种形式的继承。
    下面用代码测试一下，使用了Siesta测试框架，有兴趣可以google一下，很强大的测试系统。
 


```js
StartTest(function(t) {  
            t.diag("Extjs common test");  
            t.ok(Ext,"Ext is here");  
            Ext.define("test.Talk",  
                {  
                    talk:function()  
                        {  
                            return 'talk'  
                        }  
                }  
                );  
            Ext.define("test.Person",  
                {  
                    mixins:  
                    {  
                        everyOneNeedTalk:"test.Talk"  
                    }  
                });  
            var p = Ext.create("test.Person");  
            t.is('talk',p.talk(),'The method is mixin')  
  
            Ext.define("test.Student",{  
                config:{  
                           gender:'boy'  
                       },  
                constructor:function(config){  
                            this.initConfig(config);  
                            //这里需要调用initConfig，否则不会自动生成getter 和 setter                        
                        }  
            });  
            var s = Ext.create('test.Student')  
            t.is(s.getGender(),'boy','generate getter')  
            s.setGender('girl');  
            t.is(s.getGender(),'girl','generate setter')  
              
            t.done(); // Optional, marks the correct exit point from the test  
        });
```

引自:http://kldn.iteye.com/blog/1386622