5
我想繞過javascript模塊,但我不確定如何將模塊拆分成更多的子模塊。我已經讀過,由於性能的原因,嵌套函數並不是一個好主意,那麼如何分解模塊中的函數呢?例如,假設我有以下模塊:Javascript模塊模式,嵌套函數和子模塊
var Editor = {};
Editor.build = (function() {
var x = 100;
return {
bigFunction: function() {
// This is where I need to define a couple smaller functions
// should I create a new module for bigFunction? If so, should it be nested in Editor.build somehow?
}
};
})();
bigFunction只與Editor.build有關。我應該將構成bigFunction的較小函數附加到原型bigFunction對象嗎?我甚至不確定這是否合理。
var Editor = {};
Editor.build = (function() {
var x = 100;
return {
bigFunction: function() {
bigFunction.smallFunction();
bigFunction.prototype.smallFunction = function(){ /*do something */ };
// not sure if this even makes sense
}
};
})();
有人可以把我扔在正確的方向嗎?網上有太多誤導性的信息,並且只是像如何處理這種模塊化的明確指南。
謝謝。
它似乎並不特別適用於持有的構造及其在封閉的原型,這意味着你不能輕易稍後擴展或修改原型。很難看到標準函數聲明和原型分配的好處。模塊模式實際上是關於模塊的,即一個對象,而不是構造器。 – RobG
@RobG - 第一個肯定更多是獨立的。這和標準函數的區別在於你獲得獨特的對象。第二種方法更貼近模塊模式。我可以把第三個使用原型作爲擴展方法。 –
@RobG - 你如何在JavaScript中實現你的模塊? –