Meteor 变量范围(全局、客户端、服务器还是全部?) [英] Meteor variable scope (global, client, server, or all?)

查看:45
本文介绍了Meteor 变量范围(全局、客户端、服务器还是全部?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Meteor 的新手,我不太清楚我的变量是否可用于客户端或服务器或两者.

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 = [];
}

在这个例子中,如果我在服务器端的 variable_1 上使用 Meteor.method,我是否能够从客户端访问我刚刚对 variable_1 所做的任何事情?我可以使用 Meter.isServer 中的方法访问 variable_2 吗?variable_1 和 variable_2 的作用域有什么区别?我猜测 variable_1 可以被客户端和服务器访问,variable_2 只是客户端,而 variable_3 只是服务器.但是,我不太确定对 variable_1 范围的猜测.有人知道吗?

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?

推荐答案

这确实是一个 JavaScript 问题.所有变量都将随处可用.那是因为 JavaScript 没有块作用域.它具有某种功能范围.阅读此处的答案以了解更多信息.

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 变量范围(全局、客户端、服务器还是全部?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆