Home > Article > Web Front-end > How to convert local variables to global variables in javascript (code)
The content of this article is about the method (code) of converting local variables into global variables in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
function enjoy1(){ var num1=100; } enjoy1(); console.log(num1);//报错:num is not defined
1,
function enjoy2(){ var num2=100; window.num2=num2; } enjoy2(); console.log(num2);//100
2,
function test(){ var age=19;//局部 return function(){ console.log(age); } } var func=test();//获取局部变量,这一步不可少 func();//19
Related recommendations:
##Global variables and local variables in ScriptCase_PHP Tutorial
Detailed explanation of JavaScript variable declaration and local variable replacement of global variable usage
The above is the detailed content of How to convert local variables to global variables in javascript (code). For more information, please follow other related articles on the PHP Chinese website!