Javascript Meteor 中的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27509125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variables in Meteor
提问by Jamgreen
I have
我有
var Schemas = {};
Meteor.isClient && Template.registerHelper("Schemas", Schemas);
Schemas.Person = new SimpleSchema({
fullName: {
type: String,
index: 1,
optional: true,
},
email: {
type: String,
optional: true
},
address: {
type: String,
optional: true
},
isActive: {
type: Boolean,
},
age: {
type: Number,
optional: true
}
});
in one file and
在一个文件中
var Collections = {};
Meteor.isClient && Template.registerHelper("Collections", Collections);
Persons = Collections.Persons = new Mongo.Collection("Persons");
Persons.attachSchema(Schemas.Person);
in another file.
在另一个文件中。
I get the error ReferenceError: Schemas is not defined. It's rather obvious that I have to define Schemasin my collections.jsfile instead of having them separate. But how does Meteor work with code in separate files? I can access some objects and variables while others are unaccessible.
我收到错误ReferenceError: Schemas is not defined。很明显,我必须Schemas在我的collections.js文件中定义而不是将它们分开。但是 Meteor 如何处理单独文件中的代码?我可以访问某些对象和变量,而其他对象和变量则无法访问。
回答by Kyll
When you define a variable in the classic JavaScript way :
当您以经典的 JavaScript 方式定义变量时:
var someVar = 'someValue';
at the root of your .jsfile Meteor scopes it to the file using an IIFE.
在.js文件的根目录 Meteor 使用IIFE将其范围限定为文件。
If you want to define a global variable, simply don't write the var, giving :
如果你想定义一个全局变量,就不要写var,给出:
someVar = 'someValue';
This will define a variable in all your application by default, although you may restrict it by writing that declaration in a specific recognized folder(clientor serverfolder for example).
这将定义默认情况下,所有应用程序中的变量,虽然你可以通过写在该声明限制其特定的认可文件夹(client或server例如文件夹)。
However this variable won't be defined absolutely first. It will be defined when Meteor runs the actual code that defines it. Thus, it may not be the best practice because you're going to struggle with load order, and it will make your code dependent on how Meteor loads files: which folder you put the file in, the name of the file... Your code is prone to messy errors if you slightly touch your architecture.
但是,不会绝对首先定义此变量。当 Meteor 运行定义它的实际代码时,它将被定义。因此,这可能不是最佳实践,因为您将在加载顺序上遇到困难,并且它会使您的代码依赖于Meteor 加载文件的方式:您将文件放入哪个文件夹,文件名...您的如果您稍微触及您的架构,代码很容易出现混乱的错误。
As I suggested in another closely related postyou should go for a package directly!
正如我在另一篇密切相关的帖子中所建议的,您应该直接购买包裹!
回答by Joshua Soileau
Variables in Meteor declared with the varkeyword are scoped to the file they are declared in.
Meteor 中用var关键字声明的变量的作用域是它们声明的文件。
If you want to create a global variable do this
如果要创建全局变量,请执行此操作
Schemas = {}
回答by Michael Cole
ReferenceErroris a Node error. Meteor is a framework on top of Node.
ReferenceError是一个节点错误。Meteor 是一个基于 Node.js 的框架。
Node has a global scope (aka Node's globalvariable). This error is thrown by Node (not Meteor) if you try to access an undefined global variable.
Node 有一个全局范围(又名 Node 的global变量)。 如果您尝试访问未定义的全局变量,则 Node(而非 Meteor)会抛出此错误。
Browsers also have a global scope called window, and do not throw ReferenceErrors when undefined variables are accessed.
浏览器还有一个名为 的全局范围window,并且在访问未定义的变量时不会抛出 ReferenceErrors。
Here's a pattern I like for adding functionality to a class (it's very Meteor):
这是我喜欢为类添加功能的模式(它非常 Meteor):
/lib/Helpers.js <-- Helpers for everyone (node+browser)
/server/Helpers.js <-- Server helpers (node)
/client/Helpers.js <-- Client helpers (browser)
Consider these implementations:
考虑这些实现:
// /lib/Helpers.js
Helpers = {/* functions */}; // Assigned to window.Helpers and global.Helpers
// /server/Helpers.js
Helpers = _.extend(Helpers, {/*more functions*/}
// /client/Helpers.js
Helpers = _.extend(Helpers, {/*more functions*/}
This is a trivial example. What if I didn't want to worry about load order? Why not _.extend() in /lib/Helpers.js?
这是一个简单的例子。如果我不想担心加载顺序怎么办?为什么不在 /lib/Helpers.js 中使用 _.extend()?
// /lib/Helpers.js
// Helpers = {/* functions */}; // Overwrites...
Helpers = _.extend(Helpers, {/* functions */}); // ReferenceError
Because you'll get a ReferenceError from Node if Helpers isn't defined - specifically the "Helpers" used as an argument. (Node knows to assign Helpers as global.Helpers).
因为如果 Helpers 未定义,您将从 Node 获得 ReferenceError - 特别是用作参数的“Helpers”。(节点知道将 Helpers 分配为 global.Helpers)。
Here are two ways to "fix" this:
这里有两种方法可以“解决”这个问题:
1) Assign Helpers to something
1)为某事分配助手
// /lib/Helpers.js
// Helpers = Helpers || {} // would be another ReferenceError
if (typeof Helpers === 'undefined') Helpers = {};
Helpers = _.extend(Helpers, {/* functions */});
2) Use helpers from the global
2)使用来自全球的助手
// /lib/Helpers.js
Helpers = _.extend(global.Helpers, {/* functions */}); // works in node, but...
Both of which suck.
这两个都很烂。
1)'s syntax is horrible.
2) works in node, but there is no global in browsers. So it fails it's purpose.
1) 的语法很糟糕。
2) 在 node 中工作,但在浏览器中没有全局。所以它失败了它的目的。
So I gave up and went back to overwriting it the first time in lib, and looking for runtime errors if anything was overwritten.
所以我放弃并回到第一次在 lib 中覆盖它,并在任何被覆盖的情况下查找运行时错误。
If you have a handy cross-browser syntax for this, do comment :-) var something = something || {} something.blah = foo;
如果你有一个方便的跨浏览器语法,请评论 :-) var something = something || {} something.blah = foo;
Here's some other JS shorthand tips.
这里有一些其他的JS 速记技巧。
回答by user3807691
Session variables are global and can be accessed in different files/functions easily. Session.setPersistent is used to set the variable name persistently across all files. One might restrict from using session variables when their app is too big as they don't get deleted (hence possible memory leaks) and might give error in the console (if undefined or so). Link to the docs : https://docs.meteor.com/api/session.html
会话变量是全局的,可以很容易地在不同的文件/函数中访问。Session.setPersistent 用于在所有文件中持久设置变量名称。当他们的应用程序太大时,人们可能会限制使用会话变量,因为它们不会被删除(因此可能会出现内存泄漏)并且可能会在控制台中出现错误(如果未定义等)。链接到文档:https: //docs.meteor.com/api/session.html

