Javascrīpt沒有namespaces概念或class關鍵字, 但是對象定義卻有很多種方式。在js中使用對象往往只是用來減少對全局函數namespace的污染。
最簡單的方式是使用直接量的語法,它適合于單個的實例,比如整體布局,或窗體部件。不使用"new"來創建。
aaa = {
ccc: "test",
bbb : function() { alert(this.cc); }
}
aaa.bbb();
好處是調式的時候可以很方便的訪問內部變量。
Class definitions with private variables
aaa = (function () {
var myprivate;
return {
setIt: function(val) { myprivate = val; }
getIt: function() { return myprivate; }
}
})();
我目前的用法
function AAA() {
var myprivate;
this.setIt = function(val) { myprivate = val; }
this.getIt = function() {return myprivate; }
}
aaa = new AAA();
在extjs中,這種方法的使用很普遍。它限制了對私有變量的訪問,也造成調試的不便。
Extending objects with Ext.extend
function MyCombo (config) {
// set up your datasource here..
MyCombo.superclass.constructor.call(this,config);
}
Ext.extend(MyCombo, Ext.form.ComboBox, {
displayField:'title',
typeAhead: true,
loadingText: 'Searching
',
forceSelection: true,
allowBlank: false,
width: 160,
minChars: 1,
pageSize:10,
hideTrigger:true,
displayField: 'FullName',
valueField: 'id'
}
posted on 2007-06-19 16:48
隨波逐流 閱讀(796)
評論(0) 編輯 收藏 所屬分類:
JavaScript