問題描述
我目前正在維護大量的 JS 文件,并且依賴問題越來越嚴重.現在我將每個函數放在一個單獨的文件中,并且我手動維護一個數據庫來計算函數之間的依賴關系.
I am currently maintaining a large number of JS files and the dependency issue is growing over my head. Right now I have each function in a separate file and I manually maintain a database to work out the dependencies between functions.
這我想自動化.例如,如果我有函數 f
This I would like to automate. For instance if I have the function f
Array.prototype.f = function() {};
在另一個函數 g 中被引用
which is referenced in another function g
MyObject.g = function() {
var a = new Array();
a.f();
};
我希望能夠檢測到 g 正在引用 f.
I want to be able to detect that g is referencing f.
我該怎么做?我從哪里開始?我是否需要實際編寫一個編譯器,或者我可以調整 Spidermonkey 嗎?其他人已經這樣做了嗎?
How do I go about this? Where do I start? Do I need to actually write a compiler or can I tweak Spidermonkey for instance? Did anyone else already do this?
非常感謝任何讓我入門的指針
Any pointers to get me started is very much appreciated
謝謝多克
推薦答案
雖然理論上你可以編寫一個靜態分析工具來檢測其他文件中定義的全局變量的使用,例如 MyObject
的使用,但你無法實際跟蹤 prototype
擴展方法的使用情況.
Whilst you could theoretically write a static analysis tool that detected use of globals defined in other files, such as use of MyObject
, you couldn't realistically track usage of prototype
extension methods.
JavaScript 是一種動態類型的語言,因此任何工具都無法知道 a
,如果從 g
函數傳遞出來,是一個 Array
,因此如果在其上調用 f()
,則存在依賴關系.它只會在運行時確定哪些變量包含哪些類型,因此要找出您需要一個解釋器并且您已經讓自己成為一個圖靈完備的問題.
JavaScript is a dynamically-typed language so there's no practical way for any tool to know that a
, if passed out of the g
function, is an Array
, and so if f()
is called on it there's a dependency. It only gets determined what variables hold what types at run-time, so to find out you'd need an interpreter and you've made yourself a Turing-complete problem.
更不用說 JavaScript 的其他動態方面完全無視靜態分析,例如通過方括號表示法獲取屬性、可怕的 eval
或超時字符串或事件處理程序屬性.
Not to mention the other dynamic aspects of JavaScript that completely defy static analysis, such as fetching properties by square bracket notation, the dreaded eval
, or strings in timeouts or event handler attributes.
我認為這真的有點不適合.您可能最好手動跟蹤依賴項,但通過將相關功能分組到模塊中來簡化它,這將是您的依賴項跟蹤的基本單元.好的,您將引入更多技術上需要的功能,但希望不要太多.
I think it's a bit of a non-starter really. You're probably better of tracking dependencies manually, but simplifying it by grouping related functions into modules which will be your basic unit of dependency tracking. OK, you'll pull in a few more functions that you technically need, but hopefully not too much.
為每個模塊命名也是一個好主意,因此每個調用的去向非常清晰,從而可以輕松地手動控制依賴關系(例如,通過 //使用:ThisModule, ThatModule代碼> 頂部注釋).
It's also a good idea to namespace each module, so it's very clear where each call is going, making it easy to keep the dependencies in control manually (eg. by a // uses: ThisModule, ThatModule
comment at the top).
由于內置原型的擴展更難跟蹤,因此請將它們保持在最低限度.擴展例如.Array
在瀏覽器上包含 ECMAScript 第五版方法(如 indexOf
)是一件好事,作為所有腳本都將使用的基本修復.向現有原型添加全新的任意功能是值得懷疑的.
Since extensions of the built-in prototypes are trickier to keep track of, keep them down to a bare minimum. Extending eg. Array
to include the ECMAScript Fifth Edition methods (like indexOf
) on browsers that don't already have them is a good thing to do as a basic fixup that all scripts will use. Adding completely new arbitrary functionality to existing prototypes is questionable.
這篇關于JavaScript 依賴管理的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!