我有一个关于模块设计模式最佳实践的问题。下面的代码是我们的一些组件的编写方式的示例(我们使用 ExtJ,但这应该不重要)。我们构建了很多这样的组件,我知道这并不完全符合最佳实践。有清理代码的想法吗?
Ext.ns("TEAM.COMPONENT");
function Foo() {
// Private vars
var privateNumber=0, myButton, privateInternalObject;
var numberField = new Ext.form.NumberField({
label : 'A NumberField!',
listeners : {
'change' : function(theTextField, newVal, oldVal) {
console.log("You changed: " + oldVal + " to: " + newVal);
}
}
});
// Some private methods
function changeNumField(someNumber) {
numberField.setValue(someNumber);
}
// Some public methods
this.publicFunctionSetNumToSomething() {
changeNumField(privateNumber);
}
/**
* Initializes Foo
*/
function init() {
// Do some init stuff with variables & components
myButton = new Ext.Button({
handler : function(button, eventObject) {
console.log("Setting " + numberField + " to zero!");
changeNumField(0);
},
text : 'Set NumberField to 0'
});
privateInternalObject = new SomeObject();
word = "hello world";
privateNumber = 5;
}
init();
return this;
};
我想知道一些关于此的事情,并想询问并进行对话:
- 在声明变量时(即在 Foo 的顶部)初始化变量有多重要
- 如果此模块的客户端达到它的状态,我如何重新初始化此对象的一部分
foo
对象需要设置回其原始状态
- 这种设计可能会导致什么样的内存问题以及如何重构以减轻这种风险?
- 我可以在哪里了解更多信息?是否有任何文章可以解决这个问题,但又不会过多依赖最新和最好的 EcmaScript 5?
更新2012-05-24我只是想补充一点,我认为这个问题(Extjs:通过构造函数或 initComponent 扩展类?)与对话非常相关,特别是考虑到投票最高的答案来自“前 Ext JS 联合创始人和核心开发人员”
更新2012-05-31再补充一点,这个问题也应该链接(使用 ExtJS 扩展类时的私有成员)。另外,这是迄今为止我最喜欢的实现:
/*jshint smarttabs: true */
/*global MY, Ext, jQuery */
Ext.ns("MY.NAMESPACE");
MY.NAMESPACE.Widget = (function($) {
/**
* NetBeans (and other IDE's) may complain that the following line has
* no effect, this form is a useless string literal statement, so it
* will be ignored by browsers with implementations lower than EcmaScript 5.
* Newer browsers, will help developers to debug bad code.
*/
"use strict";
// Reference to the super "class" (defined later)
var $superclass = null;
// Reference to this "class", i.e. "MY.NAMESPACE.Widget"
var $this = null;
// Internal reference to THIS object, which might be useful to private methods
var $instance = null;
// Private member variables
var someCounter, someOtherObject = {
foo: "bar",
foo2: 11
};
///////////////////////
/* Private Functions */
///////////////////////
function somePrivateFunction(newNumber) {
someCounter = newNumber;
}
function getDefaultConfig() {
var defaultConfiguration = {
collapsible: true,
id: 'my-namespace-widget-id',
title: "My widget's title"
};
return defaultConfiguration;
}
//////////////////////
/* Public Functions */
//////////////////////
$this = Ext.extend(Ext.Panel, {
/**
* This is overriding a super class' function
*/
constructor: function(config) {
$instance = this;
config = $.extend(getDefaultConfig(), config || {});
// Call the super clas' constructor
$superclass.constructor.call(this, config);
},
somePublicFunctionExposingPrivateState: function(clientsNewNumber) {
clientsNewNumber = clientsNewNumber + 11;
somePrivateFunction(clientsNewNumber);
},
/**
* This is overriding a super class' function
*/
collapse: function() {
// Do something fancy
// ...
// Last but not least
$superclass.collapse.call(this);
}
});
$superclass = $this.superclass;
return $this;
})(jQuery);