因此,您要在外部 .js 文件中创建一堆代码,这些代码需要 jQuery 及其一些插件、MooTools,或者可能是一些更深奥的库。显然,当您加载每个脚本时,实际的“包含”是在 HEAD 部分的主机 HTML 页面中完成的。
但作为可移植性的最佳实践,您的 JavaScript .js 文件中存在哪些内置功能或广泛采用的约定,以确保下一个使用您的代码的 schmoe 记得还包含其他必需的库?
我正在寻求开发者社区的一些共识,因此请务必投票给最常见或您最熟悉的答案。
So you're creating a bunch of code in an external .js file that requires jQuery and a few of its plugins, or MooTools, or perhaps some more esoteric libraries. Obviously the actual "include" is done in the host HTML page in the HEAD section as you load in each script.
But as a best practice for portability, what built-in features, or widely-adopted conventions exist within your JavaScript .js file to ensure that the next schmoe who uses your code remembers to also include those other required libraries?
I'm looking for some consensus from the developer community, so please be sure to vote for the answer that seems most common or that you are the most familiar with.
发布评论
评论(4)
jQuery UI 在文件头中添加了其小部件的依赖项:
现在不幸的是 JavaScript 依赖项管理器的使用量比应有的要少,但如果您可以让您的库用户切换到一个,您就根本不必担心这一点:
显式检查可能也是一个好主意,因为如果某些插件可用或不可用,您可以动态地做出反应(例如,如果尚未找到 jQuery UI 对话框,则抛出异常,或者只是优雅地降级并显示一个简单的模式窗口):
这样您的脚本就不必如果不满足可选依赖项,则完全中断。
jQuery UI adds the dependencies of their widgets in the file header:
Now unfortunately JavaScript dependency managers are used way less than they should, but if you can make your libraries users switch to one you wouldn't have to worry about that at all:
Checking explicitly might be a good idea, too, since you can dynamically react if certain plugins are or are not available (e.g. either throw an exception if the jQuery UI dialog hasn't been found or just degrade gracefully and show a simple modal window):
That way your script doesn't have to break entirely if an optional dependency is not met.
对于 Visual Studio 开发人员,您可能想在标头中尝试类似的块,
虽然这不能解决您的依赖关系,但它确实记录了它们,并且它为您提供了 Visual Studio 中的智能感知...
请参阅 http://msdn.microsoft.com/en-us/library/bb385682.aspx,然后查找参考指令(没有名称或id直接链接到,抱歉......)
For the Visual Studio developers out there you may want to try blocks like these in your header
While this doesn't resolve your dependencies, it does document them, AND it gives you intellisense in Visual Studio...
See http://msdn.microsoft.com/en-us/library/bb385682.aspx, then look for References Directives (no a name or id to link directly to, sorry...)
我的 js 标头如下所示:
对于我团队中的任何人来说,任何依赖关系都非常清楚,并且这已被证明非常可靠。作为(希望)次要措施,我将始终在运行时测试这些依赖项,并在不包含脚本的情况下发出警报。
my js headers look like this:
Any dependencies are then quite clear to anyone on my team, and this has proven pretty reliable. As a (hopefully) secondary measure, I will always test for those dependencies at runtime and throw up an alert should a script not be included.
我始终相信软件工程师应该始终知道,或者至少被提醒,被迫知道他们在做什么。他们应该自己保留依赖项列表,并非常清楚为什么他/她需要它们。无论如何,一个页面中包含的 js 文件并不多。
我认为如果浏览器有 jQuery 和一些不错的插件就好了,这样我们就不需要将它们包含在页面中以节省流量。
I always believe that software engineers should always know, or at least be reminded, be forced to know what they are doing. They should keep the list of the dependencies by themselves and be very clear about why he/she needs them. There are not many js files to include in a page anyway.
I think it should be nice if the browsers have the jQuery and some nice plugins with them, so we do not need to inclcude them in in the page to save traffic though.