Meteor variable scope (global, client, server, or

2019-09-06 03:49发布

问题:

I'm new to Meteor and I don't quite get the whether my variables will be available to the client or server or both.

var variable_1 = [];
if (Meteor.isClient) {
    var variable_2 = [];
}
if (Meteor.isServer) {
    var variable_3 = [];
}

In this example if I use Meteor.method in the server side on variable_1, will I be able to access whatever I just did to variable_1 from client? Can I access variable_2 with a method in Meter.isServer? What is the difference between the scope of variable_1 and variable_2? I'm guessing that variable_1 is accessible to both client and server, variable_2 is just client, and variable_3 is just server. However, I'm quite unsure about guess on the scope of variable_1. Does anyone know?

回答1:

This is really a JavaScript question. All variables will be available everywhere. That's because JavaScript doesn't have block scoping. It has some kind of function-scoping. Read the answer here for more info.



标签: meteor scope