将javascript局部变量转换为全局变量 [英] turn javascript local variable into global variable

查看:150
本文介绍了将javascript局部变量转换为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript函数来生成var。该函数由onclick按钮事件激活。

I have a javascript function to generate a var. That function is activated by an onclick button event.

生成var后,我需要将其用作全局var,以便其他js进程可以使用该var。

After that var is generated, I need to use it as a global var so that other js processes can use that var.

我该怎么做?

推荐答案

你应该可以将变量的值添加到全局窗口的属性对象:

You should be able to add the variable's value to a property of the global window object:

window.yourVarName = yourVarName;

然后其他功能将能够访问 yourVarName 直接引用 yourVarname 。不需要使用 window.yourVarName

Then the other functions will be able to access yourVarName simply by referencing yourVarname directly. There will be no need to use window.yourVarName.

但请记住,一般来说, 全局变量是邪恶的

However keep in mind that in general, global variables are evil.

这篇关于将javascript局部变量转换为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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