是否有任何理由不在不同的源文件中提供相同的模块? [英] Is there any reason not to provide the same module in different source files?

查看:68
本文介绍了是否有任何理由不在不同的源文件中提供相同的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

// a.js
goog.provide('mypackage.a');
goog.provide('mypackage.commands');

mypackage.a.somevar = 1;

mypackage.commands.save = function(...) {...};

// b.js
goog.provide('mypackage.b');
goog.provide('mypackage.commands');

mypackage.b.somevar = 1;

mypackage.commands.read = function(...) {...};

// mypackage/commands.js
goog.provide('mypackage.commands');

mypackage.commands.runCommand = function(commandText, args) {
  return mypackage.commands[commandText](args);
}

这是提供一组可扩展的命令的好方法,还是有些东西可能会使我没有想到的复杂性?

Is this a good way to provide an extensible set of commands, or is there something that could make this complex that I'm not thinking about?

推荐答案

没有理由不能或不应该在不同的源文件中提供相同的模块.如果对您的源代码组织方案有意义,那么这是一件非常好的事情.我们拥有goog.provide()的主要原因之一是,可以在多个不同的位置使用相同的符号,但可以在碰巧先运行的文件中对其进行定义.

There is no reason that you can't or shouldn't provide the same module in different source files. If it makes sense to your source code organization scheme, then it's a perfectly fine thing to do. One of the main reasons that we have goog.provide() is so that the same symbol can be used in several different places, but defined in whichever file happens to run first.

如果我正确理解了goog.provide(),它所做的就是确保声明了一个对象.因此,goog.provide('mypackage.commands ) makes sure that mypackage.commands`在全局范围内声明.

If I understand goog.provide() correctly, all it does is make sure that an object is declared. So, goog.provide('mypackage.commands) makes sure thatmypackage.commands` is declared in the global scope.

所以goog.provide('mypackage.commands');只是完成了类似的事情:

So goog.provide('mypackage.commands'); just accomplishes something similar to this:

window.mypackage = window.mypackage || {};
window.mypackage.commands = window.mypackage.commands || {};

仅在计划向此源文件中的对象添加内容时才需要这样做.因此,如果多个源文件都在mypackage.commands上添加了新项目,则每个源文件都会执行goog.provide('mypackage.commands)`以确保声明了正确的全局变量结构.

You only need to do that when you plan on adding things to that object in this source file. So, if multiple source files are all adding new items onto mypackage.commands, then each source file would do goog.provide('mypackage.commands)` to make sure that the right global variable structure is declared.

这似乎是您在代码示例中所做的事情,这是一件非常好的事情.最好是让多个源文件都对同一个对象有所贡献(就像您一样),还是应该组织源文件以使与一个特定名称空间有关的所有代码都位于同一代码中,这是由您自己的代码组织感决定的文件.这真的取决于您,以及您认为最好的方式来组织源代码-除了正确的组织方式背后应该有一些押韵和理由外,没有正确或错误的答案.

That appears to be what you are doing in your code example and that is a perfectly fine thing to do. It is up to your own sense of code organization whether it's better to have multiple source files all contributing to the same object (like you have) or whether you should organize your source files such that all code pertaining to one particular namespace is in the same file. That's really up to you and how you think it is best to organize your source code - there is no right or wrong answer other than there should be some rhyme and reason behind how it's organized.

有用的参考文章: https://developers.google.com/closure/library/docs/tutorial

这篇关于是否有任何理由不在不同的源文件中提供相同的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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