当我定义变量时lists
如下图所示并输入lists
在控制台中,我收到错误ReferenceError: lists is not defined
var lists = new Meteor.Collection('Lists');
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "my list.";
};
Template.hello.events({
'click input' : function () {
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed the button");
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
仅当我声明时才有效lists
作为全局变量:
lists = new Meteor.Collection('Lists');
问题:为什么它必须是全球性的?
访问lists
在控制台中,您需要使用全局范围,因为控制台位于文件本身的范围之外,因为控制台被视为自己的文件。
With var
你可以访问lists
文件中的任何位置。
本质上,每个文件都包装在一个function() {..}
。这就是每个文件的变量无法在其外部访问的原因。
变量作用域存在的原因稍微复杂一些,但与第三方包/npm 模块更相关。每个包都需要有自己的作用域,不会与外部的东西发生名称冲突。
如果你想能够更正常地使用它,你可以将它放在/compatibility
文件夹也是。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)