JavaScript依赖管理 [英] JavaScript dependency management

查看:119
本文介绍了JavaScript依赖管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在维护大量的JS文件,依赖问题在我的脑海中越来越大。现在我有一个单独的文件中的每个功能,我手动维护一个数据库来解决函数之间的依赖。



我想自动化。例如,如果我有函数f

  Array.prototype.f = function(){}; 

在另一个函数中引用g

  MyObject.g = function(){
var a = new Array();
a.f();
};

我想能够检测到g正在引用f。



我该怎么做?我从哪说起呢?我需要实际编写一个编译器吗?我可以调整Spidermonkey吗?有没有其他人已经这样做了?



任何让我开始的指针都非常感谢



谢谢
Dok

解决方案

虽然理论上可以写一个静态分析工具来检测其他文件中定义的全局变量,例如使用 MyObject ,您无法实际追踪原型扩展方法的使用情况。



JavaScript是一种动态类型的语言,所以没有实用的方法可以让任何工具知道 a ,如果从 g 函数,是一个数组,所以如果 f()被调用有一个依赖。只有确定什么变量在运行时保持什么类型,所以要找出你需要一个口译员,你已经成为一个图灵完整的问题。



更不用说完全违反静态分析的JavaScript的其他动态方面,例如通过方括号表示法获取属性,可怕的 eval 或超时或事件处理程序属性中的字符串。



我认为这真是一个非启动。您可能更好地手动跟踪依赖关系,但通过将相关函数分组为将成为依赖关系跟踪的基本单位的模块进行简化。好的,你会拉几个你技术上需要的功能,但希望不会太多。



每个模块的命名空间也是个好主意,所以它是非常清除每个通话的位置,使其易于手动控制依赖关系(例如,通过 //使用:OnModule,OnModule 注释在顶部)。 p>

由于内置原型的扩展很难跟踪,将其降到最低限度。扩展例如 Array 包括ECMAScript第五版方法(如 indexOf )在浏览器上还没有它们是好的作为所有脚本将使用的基本修补程序的事情。向现有原型添加全新的任意功能是有问题的。


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.

This I would like to automate. For instance if I have the function f

Array.prototype.f = function() {};

which is referenced in another function g

MyObject.g = function() {
    var a = new Array();
    a.f();
};

I want to be able to detect that g is referencing f.

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

Thanks Dok

解决方案

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 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.

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.

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).

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依赖管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆