📅  最后修改于: 2023-12-03 15:14:57.813000             🧑  作者: Mango
Ext.js 的命名约定遵循了经典的 Model-View-Controller(MVC) 架构,使开发者可以更方便地管理和维护代码。
在 Ext.js 中,所有组件的命名都采用了缩写字母和全称单词的组合方式。
例如,Ext.Button 这个组件,它的缩写是 btn,它的全称是 button。
Ext.create('Ext.Button', {
text: 'Click me',
renderTo: Ext.getBody()
});
在命名组件时,应该使用惯例命名方式,例如 Ext.grid.Panel 代表着一个表格面板。
Ext.js 中的方法命名采用了小写字母和单词之间的下划线方式,例如 add_field。
Ext.define('MyModel', {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
],
do_something: function() {
console.log('Do something');
}
});
在 Ext.js 中,属性和变量名的命名使用的是驼峰式命名法,例如 firstName。
Ext.define('MyModel', {
config: {
firstName: null,
lastName: null
}
});
事件命名采用了小写字母和单词之间的下划线方式,例如 item_click。
Ext.define('MyView', {
extend: 'Ext.grid.Panel',
initComponent: function() {
var me = this;
me.on('itemclick', me.on_item_click, me);
me.callParent(arguments);
},
on_item_click: function(grid, record, item, index, e) {
console.log('Item clicked: ' + record.get('name'));
}
});
在 Ext.js 中,常量的命名全部采用大写字母和单词之间的下划线方式。
Ext.define('MyApp.Constants', {
statics: {
MAX_ITEMS: 100,
API_URL: 'http://example.com/api/'
}
});
在 Ext.js 中,命名约定十分重要,它不仅方便了代码的编写和维护,也提高了代码的可读性和可复用性。开发者需要在编写代码时始终遵循规范,这样才能写出高质量的 Ext.js 应用。