简体   繁体   English

将 javascript 中函数内部使用的局部变量的数据传输到全局变量/或复制到全局变量 [Javascript]

[英]Transfer data of Local variable used inside a function in javascript to Global Variable / or copy to a global Variable [Javascript]

How can i used local variable outside when it will only activate once i called it in a function.. I want to transfer value after activating it我如何在外部使用局部变量,因为它只会在我在函数中调用它时激活.. 我想在激活它后传输值


$(document).ready(function () {
 var localVariable = 'something here'
});

// now how to use localvariable it outside // 现在如何在外面使用局部变量

Create a global variable and then set the value of global variable to local variable, like this创建一个全局变量,然后将全局变量的值设置为局部变量,像这样
As document ready global variable has not any value, but when you click on document it get the value and have value of local variable and then you can call it anywhere由于文档就绪全局变量没有任何值,但是当您单击文档时,它会获取值并具有局部变量的值,然后您可以在任何地方调用它

 var globalvariable = null; $(document).ready(function(){ console.log(globalvariable); }); $(document).click(function(){ var localvariable = 20; globalvariable = localvariable; console.log(globalvariable); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM