What is the variable scope in Meteor client side?

2019-07-21 21:54发布

问题:

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.



标签: meteor scope