我可以使用全局变量代替传递参数吗? [英] Can I use global variable instead of passing parameters?

查看:65
本文介绍了我可以使用全局变量代替传递参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript项目,其脚本之一接收参数.除了在整个功能链中传递此参数之外,我还可以将其声明为全局参数,并让其他函数引用它吗?

I have a Javascript project which one of its script receive a parameter. Instead of passing this parameters throughout function chains can I declare it one as global and have other functions referring to it instead?

如果项目中有很多脚本怎么办?这些可以以某种方式访问​​此全局变量吗?我只需要将其持久化到执行期间即可.

What if there are many scripts in the project? Can these access this global variable somehow? I need to have it persistent to the duration of the execution only.

请注意,各个用户可以立即调用该项目.由于有时会通过Web应用程序调用它,因此我不确定用户缓存是否合适.

Note that this project can be called by various users and at once. As it is sometimes invoked through a webapp I am not sure User cache would be appropriate.

谢谢!

推荐答案

不要为Apps Script Services使用全局变量.例如:

Don't use Global variables for Apps Script Services. For example:

var SS_SERVICE = SpreadsheetApp;

最近这开始引起错误消息.如果情况有所变化,请在那时修改答案.

Recently this started causing an error message. If this changes, please edit the answer at that time.

此外,如果不使用 var 关键字定义变量,则该变量将自动放入全局范围.因此,如果由于错误而未能将 var 放在变量前面,则代码仍然可以运行并且可以正常工作,但是您可能不知道代码真正发生了什么.如果您在另一个函数中定义并使用了另一个具有相同名称的变量,并且错误地将该变量设为全局变量,而一个函数又将另一个函数称为另一个函数,则可能与变量值发生冲突.

Also, if you do not use the var keyword to define a variable, then it automatically gets put into the global scope. So, if by mistake, you fail to put var in front of your variable, then the code still runs and may work, but you may be unaware of what is really happening with your code. If you defined and used another variable with the same name in a different function, and also mistakenly made that variable a global, and one function called the other function, then there could be a conflict with the variable values.

所有Apps脚本 .gs 文件都可以访问所有其他 .gs 脚本文件.脚本文件之间不需要任何链接,也不必包含在其他脚本文件中.您可以从另一个脚本文件 调用一个函数,只要它在同一个项目中 即可.

All Apps Script .gs files can access all other .gs script files. There doesn't need to be any link between script files, or inclusion into other script files. You can call a function from another script file, as long as it's in the same project.

一个文件中定义的全局变量可以被其他文件访问.

And global variables defined in one file are accessible to other files.

您不想使用公共缓存来获取特定于该用户的信息.但是有私有缓存.而且Cache会过期,因此除非用于计时用户登录的时间,否则您可能不希望使用它.

You don't want to use public Cache for information specific to that user. But there is private Cache. And Cache expires, so unless it's for something like timing how long a user is logged in, you might not want to use it.

如果您有很多代码,并且由于结构有序且可以访问其他多个功能而创建函数,则可能不希望传递数据.因此,是的,您可以使用全局变量.某些人认为使用全局变量是不良做法",但随后我们开始接受个人看法.

If you have lots of code, and create functions for reasons of orderly structure and access to multiple other functions, then passing data might be undesirable. So, yes, you can use global variables. It's considered "Bad Practice" by some to use global variables, but then we are getting into personal opinion.

这篇关于我可以使用全局变量代替传递参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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