What is the variable scope in Meteor client side?

孤人 提交于 2019-12-24 03:42:57

问题


Inside the standard isClient conditional I have a variable stored. Let's say I needed to access this from the window, where would it be located?

if (Meteor.isClient) {
  var people = new Meteor.Collection("people");
}

Thanks!


回答1:


In Meteor client environment, every variable you declare without the var keyword is accessible on the global object which is window.

if (Meteor.isClient) {
  people = new Meteor.Collection("people");
  console.log(window.people._name); // displays "people" in the console
}

Variables declared with the var keyword are file scoped, variables declared without the var keyword are application scoped.

On the client, the global scope is the window object, on the server, the global scope is the global object.

When you declare a global variable in both environments, a property with this name is declared both on the window object and the global object, these two properties are distinct, if you modify the client one, it won't impact the server one.



来源:https://stackoverflow.com/questions/28868629/what-is-the-variable-scope-in-meteor-client-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!