我对模块设计模式的最佳实践有疑问。下面的代码是我们的一些组件编写方式的一个例子(我们使用ExtJs,但这不应该太重要)。我们构建了很多像这样的组件,并且我知道这完全不符合最佳实践。有任何想法来清理代码?ExtJS(JavaScript)模块设计模式最佳实践
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
对象设置回它的原件 - 什么样的内存问题可能这样的设计导致与我该如何重构来缓解那风险?
- 我在哪里可以学到更多?是否有任何文章能够解决这个问题,而不是过度依赖EcmaScript 5的最新和最大的功能?
2012-05-25 我只是想补充一点,我认为这个问题(Extjs: extend class via constructor or initComponent?)是谈话非常重要,特别是考虑到顶部投票的答案是从“前分机JS的共同创始人和核心开发”
更新2012-05-31 一个更此外,这个问题也应该链接(Private members when extending a class using 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);
[ExtJS的文档指南](http://docs.sencha.com/ext-js/4-0/# !/指南)完全覆盖了这方面的重要建议。至少看一下指南“类系统”,“组件”和“MVC应用程序体系结构”,它们将会帮助你很多。 – Tommi 2012-02-02 06:55:30