Meteor 客户端的变量范围是什么? [英] What is the variable scope in Meteor client side?

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

问题描述

在标准 isClient 条件中,我存储了一个变量.假设我需要从 window 访问它,它位于何处?

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");
}

谢谢!

推荐答案

在 Meteor 客户端环境中,您声明的每个变量没有 var 关键字都可以在全局对象 window<上访问/代码>.

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
}

使用 var 关键字声明的变量是文件范围的,没有 var 关键字声明的变量是应用程序范围的.

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

在客户端,全局作用域是window对象,在服务器端,全局作用域是global对象.

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

当你在两种环境中声明一个全局变量时,在 window 对象和 global 对象上都声明了一个同名的属性,这两个属性是不同的,修改客户端一,不会影响服务器一.

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

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